Question

I have written a method to count the number of occurrences of the words in a word file. Prior, in another method, i have sorted the words to appear in alphabetical order. There for a sample input into this method will look like this: are away birds birds going going has

My question is.. How do i delete the repeated occurrences in this method? (after counting ofcoz) I have tried to use another string array to copy the unique ones into that string array, but i get a null pointer exception.

public static String[] counter(String[] wordList)
{
    for (int i = 0; i < wordList.length; i++) 
    {
         int count = 1;
         for(int j = 0; j < wordList.length; j++)
         {
             if(i != j)  //to avoid comparing itself
             {
                 if (wordList[i].compareTo(wordList[j]) == 0)   
                 {
                     count++;
                 }
             }
         }

         System.out.println (wordList[i] + " " + count);


     }

    return wordList; 
}

Any help will be much appreciated.

Oh, and my current output looks something like this: are 1 away 1 birds 2 birds 2 going 2 going 2 has 1

Was it helpful?

Solution 2

I already posted an answer on this question. Your question is almost identical - he was having problems creating another array and getting an NPE too.

This is what I came up with (assuming the array is sorted):

public static String[] noDups(String[] myArray) { 

    int dups = 0; // represents number of duplicate numbers

    for (int i = 1; i < myArray.length; i++) 
    {
        // if number in array after current number in array is the same
        if (myArray[i].equals(myArray[i - 1]))
            dups++; // add one to number of duplicates
    }

    // create return array (with no duplicates) 
    // and subtract the number of duplicates from the original size (no NPEs)
    String[] returnArray = new String[myArray.length - dups];

    returnArray[0] = myArray[0]; // set the first positions equal to each other
                                 // because it's not iterated over in the loop

    int count = 1; // element count for the return array

    for (int i = 1; i < myArray.length; i++)
    {
        // if current number in original array is not the same as the one before
        if (!myArray[i].equals(myArray[i-1])) 
        {
           returnArray[count] = myArray[i]; // add the number to the return array
           count++; // continue to next element in the return array
        }
    }

    return returnArray; // return the ordered, unique array
}

Sample input/output:

String[] array = {"are", "away", "birds", "birds", "going", "going", "has"};

array = noDups(array);

// print the array out
for (String s : array) {
    System.out.println(s);
}

Outputs:

are
away
birds
going
has

OTHER TIPS

I would prefer using Map to store word occurrence. Keys in the map are stored in Set so it can't be duplicated. What about something like this?

public static String[] counter(String[] wordList) {
    Map<String, Integer> map = new HashMap<>();

    for (int i = 0; i < wordList.length; i++) {
        String word = wordList[i];

        if (map.keySet().contains(word)) {
            map.put(word, map.get(word) + 1);
        } else {
            map.put(word, 1);
        }
    }

    for (String word : map.keySet()) {
        System.out.println(word + " " + map.get(word));
    }

    return wordList;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top