Java Program to Find Maximum Occurring Word in a String
In this section, we will show you how to find maximum 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
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()));
for (Map.Entry<String, Integer> entry : occurrences.entrySet()) {
if (entry.getValue()==maxValue) {
System.out.println(entry.getKey());
}
}
}
}
Here the logic is simple,
First we declare the input String from which we are going to find maximum occurring word.
Convert the String to String Array by splitting the String based on delimiter(whitespace).
We are using HashMap to store key(word) and value(count of repeated word).
We are iterating over the String Array and put key and value inside HashMap. HashMap don't allow for duplicate keys. The second value should just replace the previous value.
Find maximum value in the HashMap using max() method.
Iterate over HashMap.
Finally, print the keys(words) with maximum value.
Console Output:
Java
Go
Java
Go
Example 2. Using For loop
public class Main {
public static void main(String[] args) {
String string = "php Java Python Go C# Java Python Java Go C";
String words[] = string.split(" ");
int maximumFrequency = 0;
String mostRepeatedString = null;
for (int i = 0; i < words.length; i++) {
String temp = words[i];
int count = 1;
for (int j = i + 1; j < words.length; j++) {
if (temp.equals(words[j]))
count++;
}
if (maximumFrequency < count) {
maximumFrequency = count;
mostRepeatedString = temp;
}
}
System.out.println(mostRepeatedString);
}
}
Here the logic is simple,
First we declared the input String from which we are going to find maximum occurring word.
Convert the String to String Array by splitting the String based on delimiter(whitespace).
Here we declared maximumFrequency = o and mostRepeatedString = null, based on condition these values will change.
Loop through array. Declared temp element whose value is words[i]. Declared count equal to one.
Then, Loop through array. Check whether temp element is equal to words[j]. If both are equal increment count.
Finally check whether count is greater than maximumFrequency. If so, maximumFrequency = count and mostRepeatedString = temp.
Console Output:
Java
Java
Example 3. Using Java 8 Streams
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
String string = "php Java Python Go C# Java Python Java Go C";
String mostRepeatedString = Arrays.stream(string.split(" "))
.collect(Collectors.toMap(k -> k, v -> 1,
(a, b) -> a + 1))
.entrySet().stream().max(Map.Entry.comparingByValue())
.get()
.getKey();
System.out.println(mostRepeatedString);
}
}
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
String string = "php Java Python Go C# Java Python Java Go C";
String mostRepeatedString = Arrays.stream(string.split(" "))
.collect(Collectors.toMap(k -> k, v -> 1,
(a, b) -> a + 1))
.entrySet().stream().max(Map.Entry.comparingByValue())
.get()
.getKey();
System.out.println(mostRepeatedString);
}
}
stream() method accepts a mandatory parameter array which is the array of whose elements are to be converted into a sequential stream and returns a Sequential Stream from the array passed as the parameter.
split() method is used to split the String.
collect() method collects all objects from a stream object and stored in the Map.
entrySet() returns a set view of all the entries from the Map.
max() returns the maximum element of this stream according to the provided Comparator.
comparingByValue() method returns a comparator that compares Map.Entry in natural order on value.
Console Output:
Java
Java
More related topics,