Java Array, Finding Duplicates

前端 未结 14 849
闹比i
闹比i 2020-11-22 09:13

I have an array, and am looking for duplicates.

duplicates = false;
for(j = 0; j < zipcodeList.length; j++){
    for(k = 0; k < zipcodeList.length; k++         


        
相关标签:
14条回答
  • 2020-11-22 09:40

    Don't use == use .equals.

    try this instead (IIRC, ZipCode needs to implement Comparable for this to work.

    boolean unique;
    Set<ZipCode> s = new TreeSet<ZipCode>();
    for( ZipCode zc : zipcodelist )
        unique||=s.add(zc);
    duplicates = !unique;
    
    0 讨论(0)
  • 2020-11-22 09:40

    You can also work with Set, which doesn't allow duplicates in Java..

        for (String name : names)
        {         
          if (set.add(name) == false) 
             { // your duplicate element }
        }
    

    using add() method and check return value. If add() returns false it means that element is not allowed in the Set and that is your duplicate.

    0 讨论(0)
  • 2020-11-22 09:41

    @andersoj gave a great answer, but I also want add new simple way

        private boolean checkDuplicateBySet(Integer[] zipcodeList) {
            Set<Integer> zipcodeSet = new HashSet(Arrays.asList(zipcodeList));
            if (zipcodeSet.size() == zipcodeList.length) {
                return true;
            }
            return false;
        }
    

    In case zipcodeList is int[], you need convert int[] to Integer[] first(It not auto-boxing), code here

    Complete code will be:

        private boolean checkDuplicateBySet2(int[] zipcodeList) {
            Integer[] zipcodeIntegerArray = new Integer[zipcodeList.length];
            for (int i = 0; i < zipcodeList.length; i++) {
                zipcodeIntegerArray[i] = Integer.valueOf(zipcodeList[i]);
            }
    
            Set<Integer> zipcodeSet = new HashSet(Arrays.asList(zipcodeIntegerArray));
            if (zipcodeSet.size() == zipcodeList.length) {
                return true;
            }
            return false;
        }
    

    Hope this helps!

    0 讨论(0)
  • 2020-11-22 09:42

    Print all the duplicate elements. Output -1 when no repeating elements are found.

    import java.util.*;
    
    public class PrintDuplicate {
    
        public static void main(String args[]){
            HashMap<Integer,Integer> h = new HashMap<Integer,Integer>();
    
    
            Scanner s=new Scanner(System.in);
            int ii=s.nextInt();
            int k=s.nextInt();
            int[] arr=new  int[k];
            int[] arr1=new  int[k];
            int l=0;
            for(int i=0; i<arr.length; i++)
                arr[i]=s.nextInt();
            for(int i=0; i<arr.length; i++){
                if(h.containsKey(arr[i])){
                    h.put(arr[i], h.get(arr[i]) + 1);
                    arr1[l++]=arr[i];
                } else {
                    h.put(arr[i], 1);
                }
            }
            if(l>0)
            { 
                for(int i=0;i<l;i++)
                    System.out.println(arr1[i]);
            }
            else
                System.out.println(-1);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 09:44
    public static ArrayList<Integer> duplicate(final int[] zipcodelist) {
    
        HashSet<Integer> hs = new HashSet<>();
        ArrayList<Integer> al = new ArrayList<>();
        for(int element: zipcodelist) {
            if(hs.add(element)==false) {
                al.add(element);
            }   
        }
        return al;
    }
    
    0 讨论(0)
  • 2020-11-22 09:49

    To check for duplicates you need to compare distinct pairs.

    0 讨论(0)
提交回复
热议问题