1

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++)
   {    
       for (int i = wordCount-1 ; i > j ; i--)
       {       
           if (stringArray[i].compareTo(stringArray[j]) == 0 && i!=j)
           {
               //duplicate
               duplicates++;
           }
       }
   }
   wordCount -= duplicates;
   System.out.print("\nNumber of words, not including duplicates: " + wordCount);

in the if statement, it says NullPointerException. What does this mean? Is there a better way to do this? I tried simply doing

if (stringArray[i] == stringArray[j] && i!=j)

but that kept giving me wrong answers.

1
  • what is in string array? Commented Oct 18, 2012 at 4:59

5 Answers 5

1

You can do like this for beter performance:

public int getDuplicateCount(Integer[] arr){
     int count = 0;   
     Set<Integer> set = new HashSet<Integer>();
     for (int i = 0; i < arr.length; i++) {
         if (set.contains(arr[i]))
             count++;
         set.add(arr[i]);
      }
      return count;
 }
Sign up to request clarification or add additional context in comments.

Comments

0

NullPointerException means that one of your array members is not set (i.e. it is null)

Don't use == to compare strings.

You are on the right track - chances are stringArray[] contains some members that are not set. Eacy fix is to null check before using the values.

for ( int j = 0 ; j < wordCount ; j++)
   {    
       for (int i = wordCount-1 ; i > j ; i--)
       {       
           String wordi = stringArray[i];
           String wordj = strinArray[j];
           // If both are null it won't count as a duplicate.
           // (No real need to check wordj - I do it out of habit)
           if (wordi != null && wordj != null && wordi.compareTo(wordj) == 0 && i!=j)
           {
               //duplicate
               duplicates++;
           }
       }
   }
   wordCount -= duplicates;
   System.out.print("\nNumber of words, not including duplicates: " + wordCount);

Comments

0

It means stringArray[i] is null, i.e. your array has a null entry in it somewhere. It's possible that you have a logic error elsewhere and some elements of the array are not being set correctly.

If your array legitimately contains nulls, you have to explicitly check for this before trying to call methods on stringArray[i]:

if (stringArray[i] == null){
    // Do whatever
} else if (stringArray[i].compareTo(stringArray[j]) == 0 && i!=j) {
    //duplicate
    duplicates++;
}

2 Comments

why does my array have nulls? here is the code i used to set the string array: String[] stringArray = new String[wordCount]; while (!line.equals("DONE")) { for ( int k = 0 ; k < wordCount ; k++) { //put tokens into string array StringTokenizer tokens = new StringTokenizer(line); stringArray[k] = tokens.nextToken(); } }
@Katherine I guess because line is initially "DONE" so your initialising loop never executes? Also, if line is anything other than "DONE" that loop will run forever since line is not updated in the loop body, and you should just use String.split("\\s") instead of StringTokenizer.
0

Null pointer may be because you have any null value in your array.

Your code is not working because you are itrating on same array on which you need to find duplicates

you can use following code to count duplicate words in array.

public class WordCount {


public static void main(String args[]){
    String stringArray[]={"a","b","c","a","d","b","e","f"};

    Set<String> mySet = new HashSet<String>(Arrays.asList(stringArray));

    System.out.println("Number of duplicate words: "+ (stringArray.length -mySet.size()));

    System.out.println("Number of words, not including duplicates: "+ mySet.size());
}

}

Comments

0

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<Character> 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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.