Finding repeated words on a string and counting the repetitions

Hans picture Hans · Jan 27, 2011 · Viewed 194.8k times · Source

I need to find repeated words on a string, and then count how many times they were repeated. So basically, if the input string is this:

String s = "House, House, House, Dog, Dog, Dog, Dog";

I need to create a new string list without repetitions and save somewhere else the amount of repetitions for each word, like such:

New String: "House, Dog"

New Int Array: [3, 4]

Is there a way to do this easily with Java? I've managed to separate the string using s.split() but then how do I count repetitions and eliminate them on the new string? Thanks!

Answer

Mark Peters picture Mark Peters · Jan 27, 2011

You've got the hard work done. Now you can just use a Map to count the occurrences:

Map<String, Integer> occurrences = new HashMap<String, Integer>();

for ( String word : splitWords ) {
   Integer oldCount = occurrences.get(word);
   if ( oldCount == null ) {
      oldCount = 0;
   }
   occurrences.put(word, oldCount + 1);
}

Using map.get(word) will tell you many times a word occurred. You can construct a new list by iterating through map.keySet():

for ( String word : occurrences.keySet() ) {
  //do something with word
}

Note that the order of what you get out of keySet is arbitrary. If you need the words to be sorted by when they first appear in your input String, you should use a LinkedHashMap instead.