Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Maximum occurrence problem solution

YASH PAL, 31 July 202414 February 2026
In this HackerEarth Maximum occurrence problem solution You are given a string which comprises of lower case alphabets (a-z), upper case alphabets (A-Z), numbers, (0-9), and special characters like !,-.; etc.
 
You are supposed to find out which character occurs the maximum number of times and the number of its occurrence, in the given string. If two characters occur an equal number of times, you have to output the character with the lower ASCII value.
 
For example, if your string was: aaaaAAAA, your output would be A 4, because A has a lower ASCII value than a.
 
 
HackerEarth Maximum occurrence problem solution

 

 

HackerEarth Maximum occurrence problem solution.

#include <bits/stdc++.h>

using namespace std;

int freq[3000];
char s[1005];

int main() {
int maxFreq = 0, n;
char ch;
cin.getline(s, 1000);
n = strlen(s);
for (int i = 0; i < n; ++i) {
freq[s[i]]++;
if (freq[s[i]] > maxFreq) {
maxFreq = freq[s[i]];
ch = s[i];
} else if (freq[s[i]] == maxFreq && ch > s[i]) {
ch = s[i];
}
}
cout << ch << " " << maxFreq;
return 0;
}
 

Second solution

s = raw_input()
assert(len(s)>0 and len(s)<1001)
ans = [0]*300
for i in s:
ans[ord(i)]+=1
final, mx = "", 0
for i in xrange(len(ans)):
if ans[i] > mx:
mx = ans[i]
final = chr(i)
print final, mx
 
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes