Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

HackerEarth Maximum occurrence problem solution

YASH PAL, 31 July 2024
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

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes