Posts

Showing posts with the label Find Maximum Occurring Word in a String

Java Program to Find Maximum Occurring Word in a String

Image
In this section, we will show you how to find m aximum occurring word in a string . 1.  Using For loop and Map 2.  Using For loop 3. Using  Java 8 Streams Example 1. Using For loop and Map Note: We may have more than one key with the same maximum value. This program  will print all the keys with maximum value.   import java.util.Collections ; import java.util.HashMap ; import java.util.Map ; public class Main { public static void main ( String [] args) { String string = "Java Python Go C# Java Python Java Go C Go" ; String words [] = string .split( " " ); Map < String , Integer > occurrences = new HashMap<>(); for ( String word : words ) { Integer oldCount = occurrences .get( word ); if ( oldCount == null ) { oldCount = 0 ; } occurrences .put( word , oldCount + 1 ); } int maxValue =( Collections . max ( occurrences .values())); ...