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!
You’ve got the hard work done. Now you can just use a
Mapto count the occurrences:Using
map.get(word)will tell you many times a word occurred. You can construct a new list by iterating throughmap.keySet():Note that the order of what you get out of
keySetis arbitrary. If you need the words to be sorted by when they first appear in your input String, you should use aLinkedHashMapinstead.