7

I am trying to write a program which will generate a random ten integer array(integers between 1 and 6) and then I have to form another array with all duplicates removed. So {1,3,5,5,3,4,2,2,2,1} should return {1,3,5,4,2}. The problem is that I get an answer but the output array contains 0s in the places where the duplicates were and I do not know how to decrease the length of the temp array(if it is even possible). Here is my program.:

import java.util.*;
public class Lab9Tut12{
public static void main (String[]args){
    int [] numbers = new int[10];
    //int length = 10;
    int[] temp = new int[length];
    for(int i=0;i<10;i++){
        numbers [i] = (int)(Math.random()*6+1);
        System.out.print(numbers [i]);
        System.out.println();
    }
    for(int i=1;i<10;i++){
       if(numbers[i-1]!=numbers[i]){
         temp[i]= numbers[i];
         //length--;
       }
    }
    System.out.println(Arrays.toString(temp));
}

}

Georgi Koemdzhiev
  • 11,421
  • 18
  • 62
  • 126
  • You can't decrease array length, because it has a fixed size that can't be change at the run time. – Salah Mar 12 '14 at 13:27
  • do you need the first array? if not you can verify when you input the new value for duplicates. Or you can use a Set (doesn't allow duplicates). – Edwin Mar 12 '14 at 13:29
  • You should accept an answer if any of them helped you. – Cruncher Mar 12 '14 at 13:49

5 Answers5

14

A nice way to do this is to utilize a Set. That's a structure, that contains only unique values.

Set<Integer> set = new HashSet<Integer>();
int[] array = {1,1,2,2,2,3,3,4,5,6,8};

for (int num : array) {
    set.add(num);
}

System.out.println(set);

Outputs:

[1, 2, 3, 4, 5, 6, 8]

To convert the set to an array you can use set.toArray().

Warlord
  • 2,798
  • 16
  • 21
  • That's really good, as the add method wont add an integer if it's already in the set. Cheers! :) – Georgi Koemdzhiev Mar 12 '14 at 13:36
  • 2
    Danger! Iteration order in a `HashSet` is _not_ guaranteed. You want to use a `LinkedHashSet` if iteration order matters. – fge Mar 12 '14 at 13:45
4

Use Set instead. Put all the array values in a set and then convert back to array.

Set<Integer> numbersSet = new HashSet<>(Arrays.asList(numbers));

Integer[] uniqueNumbers = numbersSet.toArray(new Integer[0]);

Set will eliminate all you duplicates and you don't need to do anything for it. Just put the numbers there.

Vishal Nair
  • 2,141
  • 3
  • 26
  • 39
Avi
  • 21,182
  • 26
  • 82
  • 121
2

You could use a Set to store your unique random numbers. Set API

Set<Integer> set = new HashSet<Integer>();
set.add(randomNumber);
...

Later convert to a list:

 List<Integer> list = new ArrayList<Integer>(set);
Klemens Morbe
  • 595
  • 9
  • 24
1

Try use this piece of code. Set does not allow you to put 2 same objects.

import java.util.HashSet;
import java.util.Set;

public class MyClass {

    public static void main(String[] args) {

        int size = 10;
        Set<Integer> numbers = new HashSet<Integer>();

        for (int i = 0; i < size; i++) {
            numbers.add((int) (Math.random() * 6 + 1));
        }

        System.out.println(numbers);

    }
}
ruhungry
  • 4,506
  • 20
  • 54
  • 98
1

Using a Set is nice, however you'll have a problem: its .toArray() will return an Integer[], not an int[] (and you cannot have a Set<int>).

Here is a solution which still uses a set, but differently:

public static int[] onlyUniqueElements(final int[] inputArray)
{
    final Set<Integer> set = new HashSet<>();
    final int[] tmp = new int[inputArray.length];
    int index = 0;
    for (final int i: inputArray)
        if (set.add(i))
            tmp[index++] = i;

    return Arrays.copyOfRange(tmp, 0, index);
}
fge
  • 119,121
  • 33
  • 254
  • 329