Counting repeated elements in an integer array

前端 未结 13 1324
挽巷
挽巷 2020-11-27 08:02

I have an integer array crr_array and I want to count elements, which occur repeatedly. First, I read the size of the array and initialize it with numbers read

13条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 08:27

     public static void duplicatesInteger(int arr[]){
        Arrays.sort(arr);       
        int count=0;
        Set s=new HashSet();
        for(int i=0;i<=arr.length-1;i++){
            for(int j=i+1;j<=arr.length-1;j++){
                if(arr[i]==arr[j] && s.add(arr[i])){
                    count=count+1;              
                                    }
            }
            System.out.println(count);
        }
    }
    

提交回复
热议问题