Skip to content

Instantly share code, notes, and snippets.

@Sam-Kruglov
Created June 7, 2018 05:47
Show Gist options
  • Save Sam-Kruglov/6bb0122644cd20148e2fce2a19dfff79 to your computer and use it in GitHub Desktop.
Save Sam-Kruglov/6bb0122644cd20148e2fce2a19dfff79 to your computer and use it in GitHub Desktop.
Finds the most repeated word in a text
public class RepeatedWordFinder {
public static void main(String[] args) {
System.out.println(findTheMostRepeatedWord("House, House, House, Dog, Dog, Dog, Dog, cat,cat"));
}
public static String findTheMostRepeatedWord(String s) {
return Arrays.stream(s.toLowerCase().trim().split("[\\n\\t\\r.,;:!?()]"))
.map(String::trim).collect(Collectors.groupingBy(w -> w, Collectors.counting()))
.entrySet().stream().max(Map.Entry.comparingByValue()).get().getKey();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment