Given a string s of lowercase alphabets. The task is to find the maximum occurring character in the string s. If more than one character occurs the maximum number of times then print the lexicographically smaller character.
Examples:
Input: s="geeksforgeeks"
Output: 'e'
Explanation: 'e' occurs 4 times in the stringInput: s="test"
Output: 't'
Explanation: 't' occurs 2 times in the string
Try It Yourself
Table of Content
[Expected Approach] Frequency Array - O(n) Time and O(1) Space
Since the input string contains only lowercase English letters ('a' to 'z'), we can use a fixed-size frequency array of size 26.
- Traverse the string to populate the counts of each character.
- Loop from index 0 to 25 (corresponding to 'a' to 'z').
- By using a strictly greater than condition (> maxFreq), any character encountered later with the same frequency is ignored, which automatically handles the lexicographical tie-breaking rule.
#include <iostream>
#include <string>
using namespace std;
char getMaxOccuringChar(string s) {
int freq[26] = {0};
for (char ch : s) {
freq[ch - 'a']++;
}
char maxChar = 'a';
int maxFreq = 0;
for (int i = 0; i < 26; i++) {
if (freq[i] > maxFreq) {
maxFreq = freq[i];
maxChar = 'a' + i;
}
}
return maxChar;
}
int main() {
string s = "testsample";
cout << getMaxOccuringChar(s) << endl;
return 0;
}
public class GFG {
public static char getMaxOccuringChar(String s) {
int[] freq = new int[26];
for (int i = 0; i < s.length(); i++) {
freq[s.charAt(i) - 'a']++;
}
char maxChar = 'a';
int maxFreq = 0;
for (int i = 0; i < 26; i++) {
if (freq[i] > maxFreq) {
maxFreq = freq[i];
maxChar = (char) ('a' + i);
}
}
return maxChar;
}
public static void main(String[] args) {
String s = "testsample";
System.out.println(getMaxOccuringChar(s));
}
}
def getMaxOccuringChar(s):
freq = [0] * 26
for ch in s:
freq[ord(ch) - ord('a')] += 1
max_char = 'a'
max_freq = 0
for i in range(26):
if freq[i] > max_freq:
max_freq = freq[i]
max_char = chr(ord('a') + i)
return max_char
if __name__ == "__main__":
s = "testsample"
print(getMaxOccuringChar(s))
using System;
class GFG {
public static char getMaxOccuringChar(string s) {
int[] freq = new int[26];
foreach (char ch in s) {
freq[ch - 'a']++;
}
char maxChar = 'a';
int maxFreq = 0;
for (int i = 0; i < 26; i++) {
if (freq[i] > maxFreq) {
maxFreq = freq[i];
maxChar = (char)('a' + i);
}
}
return maxChar;
}
public static void Main(string[] args) {
string s = "testsample";
Console.WriteLine(getMaxOccuringChar(s));
}
}
function getMaxOccuringChar(s) {
const freq = new Array(26).fill(0);
for (let i = 0; i < s.length; i++) {
freq[s.charCodeAt(i) - 97]++;
}
let maxChar = 'a';
let maxFreq = 0;
for (let i = 0; i < 26; i++) {
if (freq[i] > maxFreq) {
maxFreq = freq[i];
maxChar = String.fromCharCode(97 + i);
}
}
return maxChar;
}
// Driver code
const s = "testsample";
console.log(getMaxOccuringChar(s));
Output
e
[Alternate Approach] Using Hash Map - O(n) Time and O(1) Space
We use a Hash Map (or Unordered Map) to store character frequencies.
- Traverse the string to populate the counts of each character in a Hash Map or Dictionary
- Loop from 'a' to 'z' and compare frequencies with the max so far.
- By using a strictly greater than condition (> maxFreq), any character encountered later with the same frequency is ignored, which automatically handles the lexicographical tie-breaking rule.
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
char getMaxOccuringChar(string s) {
unordered_map<char, int> countMap;
for (char ch : s) {
countMap[ch]++;
}
char maxChar = 'a';
int maxFreq = 0;
for (char ch = 'a'; ch <= 'z'; ch++) {
if (countMap.find(ch) != countMap.end() && countMap[ch] > maxFreq) {
maxFreq = countMap[ch];
maxChar = ch;
}
}
return maxChar;
}
int main() {
string s = "testsample";
cout << getMaxOccuringChar(s) << endl;
return 0;
}
import java.util.HashMap;
public class GFG {
public static char getMaxOccuringChar(String s) {
HashMap<Character, Integer> countMap = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
countMap.put(ch, countMap.getOrDefault(ch, 0) + 1);
}
char maxChar = 'a';
int maxFreq = 0;
for (char ch = 'a'; ch <= 'z'; ch++) {
if (countMap.containsKey(ch) && countMap.get(ch) > maxFreq) {
maxFreq = countMap.get(ch);
maxChar = ch;
}
}
return maxChar;
}
public static void main(String[] args) {
String s = "testsample";
System.out.println(getMaxOccuringChar(s));
}
}
def getMaxOccuringChar(s):
count_map = {}
for ch in s:
count_map[ch] = count_map.get(ch, 0) + 1
max_char = 'a'
max_freq = 0
for ch in (chr(i) for i in range(ord('a'), ord('z') + 1)):
if ch in count_map and count_map[ch] > max_freq:
max_freq = count_map[ch]
max_char = ch
return max_char
if __name__ == "__main__":
s = "testsample"
print(getMaxOccuringChar(s))
using System;
using System.Collections.Generic;
class GFG {
public static char getMaxOccuringChar(string s) {
Dictionary<char, int> countMap = new Dictionary<char, int>();
foreach (char ch in s) {
if (countMap.ContainsKey(ch)) {
countMap[ch]++;
} else {
countMap[ch] = 1;
}
}
char maxChar = 'a';
int maxFreq = 0;
for (char ch = 'a'; ch <= 'z'; ch++) {
if (countMap.ContainsKey(ch) && countMap[ch] > maxFreq) {
maxFreq = countMap[ch];
maxChar = ch;
}
}
return maxChar;
}
public static void Main(string[] args) {
string s = "testsample";
Console.WriteLine(getMaxOccuringChar(s));
}
}
function getMaxOccuringChar(s) {
const countMap = {};
for (let i = 0; i < s.length; i++) {
const ch = s[i];
countMap[ch] = (countMap[ch] || 0) + 1;
}
let maxChar = 'a';
let maxFreq = 0;
for (let i = 0; i < 26; i++) {
const ch = String.fromCharCode(97 + i);
if (countMap[ch] !== undefined && countMap[ch] > maxFreq) {
maxFreq = countMap[ch];
maxChar = ch;
}
}
return maxChar;
}
// Driver code
const s = "testsample";
console.log(getMaxOccuringChar(s));
Output
e