Compare items in an array for duplicates

I am trying to create a 5 digit array intin Java and I am having trouble getting started. None of the numbers in the array can be duplicate. I can generate random numbers for this, but just can't figure out how to compare numbers with each other and replace any duplicates.

+5
source share
8 answers

You can use java.util.Set instead of an array, as it has only unique elements.

+10
source

If I understand you correctly, do you need a random 5-digit number, without repeating the number?

If so, one way is to shuffle the list of digits 0-9, and then select the first 5 elements.

EDIT

Integer[] digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Random random = new Random();

public Integer[] generateId() {
    List<Integer> id = Arrays.asList(digits);
    Collections.shuffle(id, random);
    return id.subList(0, 5).toArray(new Integer[0]);
}
+4

, TreeSet ( ):

int numbers[] { 4 5 7 6 5 7 5 89 847 7 94 093 02 10 11 10 11 };
TreeSet set new TreeSet(Arrays.asList(numbers));
for (int no : set)
  System.out.println(no);
+2

O ( ), , < - ,

int[] digits = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Random random = new Random();

int[] generateId() {
    int[] choices = digits.clone();
    int[] id = new int[5];

    for (int i = 0; i < 5; i++) {
        // one less choice to choose from each time
        int index = random.nextInt(choices.length - i);
        id[i] = choices[index];
        // "remove" used item by replacing it with item at end of range
        // because that index at the end won't be considered in next round
        choices[index] = choices[choices.length - i - 1];
    }

    return id;
}
+2

:

int[] digits = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Random random = new Random();

int[] generateId() {
    int[] clone = digits.clone();
    int[] id = new int[5];

    for (int i = 0; i < 5; i++) {
        int candidate;

        do {
            candidate = random.nextInt(10);
        } while (clone[candidate] == -1);

        id[i] = clone[candidate];
        clone[candidate] = -1;
    }

    return id;
}
+1

.

    List<int> myList = new List<int>() { 1, 1, 2, 3, 4, 5, 5, 7 , 1, 7};
    myList.Sort();
    for (int i = myList.Count - 1; i > 0; i--)
    {
        if (myList[i] == myList[i - 1])
            myList.RemoveAt(i);
    }

, , .

+1

, , .

, , , , . . .

do
    {
        for (int i = 0; i < 5; i++) 
        {
            iNumber = generator.nextInt(9) + 1;
            numbers[i] = iNumber;
        }
    }
    while(numbers[0] == numbers[1] || numbers[0] == numbers[2] || numbers[0] == numbers[3] || numbers[0] == numbers[4] || numbers[1] == numbers[2] || numbers[1] == numbers[3] || numbers[1] == numbers[4] || numbers[2] == numbers[3] || numbers[2] == numbers[4] || numbers[3] == numbers[4]);
0
/**
 * findDuplicate method return map where key is unique no and value as the
 * repitation
 * 
 * @param a
 *            : arrays of Objects
 * @return map
 */
public Map findDuplicate(T[] a) {
    Map<T, Integer> map = new HashMap<T, Integer>();
    Set<T> unique = new HashSet<T>(Arrays.asList(a));
    int count = 0;
    for (T integer : unique) {
        for (T integer1 : a) {
            if (integer == integer1) {
                ++count;
            }
        }
        map.put(integer, count);
        count = 0;
    }

    return map;
}
0

All Articles