how to compare elements in a string array in java?

后端 未结 5 1602
予麋鹿
予麋鹿 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 13:00

    Here i see you are trying to find unique elements count for given string. I would suggest using HashSet for better solution.

    public int getUniqueElements(String str)
    {
      HashSet hSet = new HashSet<>();
    
      // iterate given string, hSet only adds unique elements to hashset
      for(int i = 0; i < str.length() ; i++
        hSet.add(str.charAt(i));
    
      return hSet.size();
    }
    

提交回复
热议问题