C# program to get max occurred character in a String

To find the maximum occurred character in a string in C#, we need to count the frequency of each character and then identify which character appears most frequently. This can be achieved using different approaches including character arrays and dictionaries.

Syntax

Using a character frequency array −

int[] frequency = new int[256]; // ASCII characters
for (int i = 0; i 

Using a dictionary for character counting −

Dictionary<char, int> charCount = new Dictionary<char, int>();
foreach (char c in str) {
    charCount[c] = charCount.ContainsKey(c) ? charCount[c] + 1 : 1;
}

Using Character Array Approach

This approach uses a fixed-size array to count character frequencies based on ASCII values −

using System;

class Program {
    static int maxCHARS = 256;

    static char GetMaxOccurredChar(string s) {
        int[] frequency = new int[maxCHARS];
        
        // Count frequency of each character
        for (int i = 0; i  maxCount) {
                maxCount = frequency[i];
                maxChar = (char)i;
            }
        }
        
        return maxChar;
    }

    public static void Main() {
        string s = "livelife!";
        char maxChar = GetMaxOccurredChar(s);
        
        // Count occurrences of the max character
        int count = 0;
        foreach (char c in s) {
            if (c == maxChar) count++;
        }
        
        Console.WriteLine("String: " + s);
        Console.WriteLine("Maximum occurred character: " + maxChar);
        Console.WriteLine("Occurrence: " + count + " times");
    }
}

The output of the above code is −

String: livelife!
Maximum occurred character: e
Occurrence: 2 times

Using Dictionary Approach

This approach uses a dictionary which is more memory-efficient for strings with limited unique characters −

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
    static char GetMaxOccurredChar(string s) {
        Dictionary<char, int> charCount = new Dictionary<char, int>();
        
        // Count frequency of each character
        foreach (char c in s) {
            if (charCount.ContainsKey(c))
                charCount[c]++;
            else
                charCount[c] = 1;
        }
        
        // Find character with maximum frequency
        return charCount.OrderByDescending(x => x.Value).First().Key;
    }

    public static void Main() {
        string s = "programming";
        char maxChar = GetMaxOccurredChar(s);
        
        // Count occurrences
        int count = s.Count(c => c == maxChar);
        
        Console.WriteLine("String: " + s);
        Console.WriteLine("Maximum occurred character: " + maxChar);
        Console.WriteLine("Occurrence: " + count + " times");
    }
}

The output of the above code is −

String: programming
Maximum occurred character: m
Occurrence: 2 times

Finding All Characters with Maximum Frequency

Sometimes multiple characters may have the same maximum frequency. Here's how to handle such cases −

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
    static void FindAllMaxOccurredChars(string s) {
        Dictionary<char, int> charCount = new Dictionary<char, int>();
        
        // Count frequency of each character
        foreach (char c in s) {
            charCount[c] = charCount.ContainsKey(c) ? charCount[c] + 1 : 1;
        }
        
        // Find maximum frequency
        int maxFreq = charCount.Values.Max();
        
        // Get all characters with maximum frequency
        var maxChars = charCount.Where(x => x.Value == maxFreq).ToList();
        
        Console.WriteLine("String: " + s);
        Console.WriteLine("Maximum frequency: " + maxFreq);
        Console.WriteLine("Characters with maximum frequency:");
        
        foreach (var item in maxChars) {
            Console.WriteLine("Character '" + item.Key + "' occurs " + item.Value + " times");
        }
    }

    public static void Main() {
        string s = "aabbcc";
        FindAllMaxOccurredChars(s);
    }
}

The output of the above code is −

String: aabbcc
Maximum frequency: 2
Characters with maximum frequency:
Character 'a' occurs 2 times
Character 'b' occurs 2 times
Character 'c' occurs 2 times

Comparison of Approaches

Approach Time Complexity Space Complexity Best For
Character Array O(n) O(1) - Fixed 256 size ASCII characters, fixed memory
Dictionary O(n) O(k) - k unique characters Unicode characters, memory efficiency
LINQ with Dictionary O(n log k) O(k) Readable code, multiple max chars

Conclusion

Finding the maximum occurred character in a string can be efficiently achieved using character frequency counting. The character array approach offers constant space complexity for ASCII characters, while the dictionary approach provides better memory efficiency for strings with fewer unique characters and supports Unicode characters.

Updated on: 2026-03-17T07:04:35+05:30

926 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements