Compare elements in an array for duplicates

2020-02-14 03:35发布

问题:

I am trying to generate a 5 digit int array in Java and am having trouble on where to start. None of the numbers in the array can be duplicates. I can generate random numbers for it fine but just cant figure out how to compare the numbers to each other and replace any duplicates.

回答1:

You can use a java.util.Set instead of an array as it is guaranteed to have only unique elements.



回答2:

If I understand you correctly, you want a random 5 digit number, with no digit repeated?

If so, one way is to shuffle a list of the digits 0-9, then pick 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]);
}


回答3:

You can get rid of the duplicates by converting the array into a TreeSet (that will also sort them):

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);


回答4:

this generates it in O(number of digits), no inner loops, no shuffling <- this could be expensive if the number of choices gets really big

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;
}


回答5:

try this:

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;
}


回答6:

As an alternative, just sort the array and iterate it.

    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);
    }

But of course it's best not to get any duplicates to start with.



回答7:

First off i want to thank all of you for helping out i really appreciate it.

I got this program to work the way i want but it seems like there should be an easier way though. Here is what i did. Any more comments would be awesome.

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]);


回答8:

/**
 * 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;
}


标签: java arrays