In this article, you will learn how to find the maximum occurring character in a given string using Java. To find the maximum occurring character in a given string, you can iterate through the string and keeps track of the frequency of each character. Once you have the frequency of each character, you can identify the character with the highest occurrence.
Java programs to find the maximum occurring character in String
Here are a few different solutions to find the maximum occurring character in a given string using Java:
1. Using HashMap
In this program, we use a HashMap
to store the frequency of each character encountered in the input string. We then iterate through the characters, ignoring non-alphabetic characters, and update the frequency count in the charFrequencyMap
. Finally, we find the character with the highest frequency and return it as the maximum occurring character.
Keep in mind that this implementation is case-insensitive, meaning it treats uppercase and lowercase letters as the same. If you want a case-sensitive version, simply remove the input = input.toLowerCase();
line from the code.
package com.javacodepoint.string;
import java.util.HashMap;
import java.util.Map;
public class MaxOccurringCharacter {
public static char findMaxOccurringChar(String input) {
if (input == null || input.isEmpty()) {
throw new IllegalArgumentException("Input string is null or empty.");
}
// Create a HashMap to store character frequencies
Map<Character, Integer> charFrequencyMap = new HashMap<>();
// Convert the input string to lowercase to make it case-insensitive
input = input.toLowerCase();
// Iterate through the string and count the occurrences of each character
for (char c : input.toCharArray()) {
// Ignore non-alphabetic characters, you can modify this condition based on your
// requirements
if (Character.isAlphabetic(c)) {
charFrequencyMap.put(c, charFrequencyMap.getOrDefault(c, 0) + 1);
}
}
// Find the maximum occurring character and its frequency
char maxChar = '\0'; // Initialize maxChar to a null character
int maxFrequency = 0;
for (Map.Entry<Character, Integer> entry : charFrequencyMap.entrySet()) {
int frequency = entry.getValue();
if (frequency > maxFrequency) {
maxFrequency = frequency;
maxChar = entry.getKey();
}
}
return maxChar;
}
public static void main(String[] args) {
String inputString = "Javacodepoint";
char maxChar = findMaxOccurringChar(inputString);
System.out.println("Maximum occurring character: " + maxChar);
}
}
OUTPUT:
Maximum occurring character: a
2. Using Array
In this program, we use an integer array
to store the frequency of each character encountered in the input string.
package com.javacodepoint.string;
/*
* Find maximum occurring character using Array
*/
public class FindMaximumOccurringChar2 {
// method to find maximum occurring character
static char findMaxOccurringChar(String str) {
// count array to hold the character count
int count[] = new int[26];
// count the character without checking case
str = str.toLowerCase();
// to store maximum occurring char ASCII no
char maxOccuringChar = 0;
// to store max count
int maxCount = 0;
// iterate the string
for (int i = 0; i < str.length(); i++) {
// pick a character and convert in ASCII no
int ch = (int) str.charAt(i);
// 97 is the ascii value for 'a'
count[ch - 97] = count[ch - 97] + 1;
// update the max count
if (maxCount < count[ch - 97]) {
maxOccuringChar = (char) ch;
maxCount = count[ch - 97];
}
}
return maxOccuringChar;
}
// main method to test
public static void main(String[] args) {
System.out.println("Find Maximum Occurring Character Example");
System.out.println("Maximum Occurring Character in \"Java\" => " + findMaxOccurringChar("Java"));
System.out.println("Maximum Occurring Character in \"Programming\" => " + findMaxOccurringChar("Programming"));
System.out.println("Maximum Occurring Character in \"Example\" => " + findMaxOccurringChar("Example"));
}
}
OUTPUT:
Find Maximum Occurring Character Example
Maximum Occurring Character in “Java” => a
Maximum Occurring Character in “Programming” => r
Maximum Occurring Character in “Example” => e