Created
June 7, 2018 05:47
-
-
Save Sam-Kruglov/6bb0122644cd20148e2fce2a19dfff79 to your computer and use it in GitHub Desktop.
Finds the most repeated word in a text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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