how to compare elements in a string array in java?

后端 未结 5 1588
予麋鹿
予麋鹿 2021-01-07 12:26

I am trying to find duplicate words in a string array.

Here is my code for the comparison:

   for ( int j = 0 ; j < wordCount ; j++)
   {    
             


        
5条回答
  •  抹茶落季
    2021-01-07 12:49

    You can do like this for beter performance:

    public int getDuplicateCount(Integer[] arr){
         int count = 0;   
         Set set = new HashSet();
         for (int i = 0; i < arr.length; i++) {
             if (set.contains(arr[i]))
                 count++;
             set.add(arr[i]);
          }
          return count;
     }
    

提交回复
热议问题