Compare elements in an array for duplicates

后端 未结 8 739
轻奢々
轻奢々 2020-12-21 00:34

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

相关标签:
8条回答
  • 2020-12-21 00:55

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

    0 讨论(0)
  • 2020-12-21 00:59

    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);
    
    0 讨论(0)
  • 2020-12-21 00:59

    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.

    0 讨论(0)
  • 2020-12-21 01:01

    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]);
    }
    
    0 讨论(0)
  • 2020-12-21 01:04

    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;
    }
    
    0 讨论(0)
  • 2020-12-21 01:05
    /**
     * 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 讨论(0)
提交回复
热议问题