简体   繁体   English

比较数组中的元素是否重复

[英]Compare elements in an array for duplicates

I am trying to generate a 5 digit int array in Java and am having trouble on where to start. 我试图在Java中生成一个5位数的int数组,并且无法从哪里开始。 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. 我可以为它生成随机数,但是无法弄清楚如何将数字相互比较并替换任何重复项。

您可以使用java.util.Set而不是数组,因为它保证只有唯一元素。

If I understand you correctly, you want a random 5 digit number, with no digit repeated? 如果我理解正确,你想要一个随机的5位数字,没有数字重复?

If so, one way is to shuffle a list of the digits 0-9, then pick the first 5 elements. 如果是这样,一种方法是将数字列表0-9混洗,然后选择前5个元素。

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

You can get rid of the duplicates by converting the array into a TreeSet (that will also sort them): 您可以通过将数组转换为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);

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 这会产生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;
}

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

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. 但当然最好不要重复开始。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM