-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaximum_occuring_char.cpp
41 lines (35 loc) · 972 Bytes
/
maximum_occuring_char.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
Link:- https://practice.geeksforgeeks.org/problems/maximum-occuring-character-1587115620/1
problem:- Given a string str. The task is to find the maximum occurring character in the string str. If more than one character occurs the maximum number of time then print the lexicographically smaller character.
Input:
str = testsample
Output: e
Explanation: e is the character which
is having the highest frequency.
Input:
str = output
Output: t
Explanation: t and u are the characters
with the same frequency, but t is
lexicographically smaller.
*/
char getMaxOccuringChar(string str)
{
// Your code here
map<char,int>m;
for(int i=0;i<str.length();i++)
{
m[str[i]]++;
}
int max=0;
char c;
for(auto i=m.begin();i!=m.end();i++)
{
if(max<i->second)
{
max=i->second;
c=i->first;
}
}
return c;
}