Java Program to Find Maximum Occurring Character in String

1. Introduction

Finding the maximum occurring character in a string is a common problem in string manipulation and analysis. This task involves counting the frequency of each character in a string and identifying the character with the highest count. Such problems are foundational in understanding how to work with arrays, strings, and data structures like hash maps in Java. This blog post will demonstrate a Java program to find the maximum occurring character in a given string.

2. Program Steps

1. Define the input string.

2. Create a frequency array or a hash map to keep track of each character's occurrences.

3. Iterate through the string, updating the frequency of each character.

4. Find the character with the maximum frequency.

5. Display the maximum occurring character.

3. Code Program

import java.util.HashMap;
import java.util.Map;

public class MaxOccurringChar {
    public static void main(String[] args) {
        String input = "sample string";
        Map<Character, Integer> charFrequencyMap = new HashMap<>();

        // Counting each character's frequency
        for (char ch : input.toCharArray()) {
            charFrequencyMap.put(ch, charFrequencyMap.getOrDefault(ch, 0) + 1);
        }

        char maxOccurringChar = ' ';
        int maxFrequency = 0;

        // Finding the character with the maximum frequency
        for (Map.Entry<Character, Integer> entry : charFrequencyMap.entrySet()) {
            if (entry.getValue() > maxFrequency) {
                maxFrequency = entry.getValue();
                maxOccurringChar = entry.getKey();
            }
        }

        // Displaying the result
        System.out.println("Maximum occurring character: '" + maxOccurringChar + "' with a frequency of " + maxFrequency);
    }
}

Output:

Maximum occurring character: 's' with a frequency of 2

Explanation:

1. The program starts by defining an input string input to analyze.

2. A HashMap named charFrequencyMap is created to store the frequency of each character in the string. The key is the character, and the value is its frequency.

3. The program converts the input string to a character array and iterates through it. For each character, it updates the charFrequencyMap with the new frequency count using the getOrDefault method, which returns the current frequency of the character or 0 if it is not yet in the map.

4. After counting the frequencies of all characters, the program iterates through the charFrequencyMap to find the character with the maximum frequency. It keeps track of the character and its frequency using maxOccurringChar and maxFrequency variables.

5. Once the character with the highest frequency is found, the program prints the character and its frequency.

6. This approach efficiently finds the maximum occurring character by using a hash map to store frequency counts and only requires a single pass through the string to count frequencies and another pass through the map to find the maximum.

Comments