TechoSagar: Maximum Occuring Character

Search

Google Alert - jobs

Maximum Occuring Character

Given a string, find the maximum occurring character in the string. If more than one character occurs maximum number of time then print the lexicographically smaller character.
Input:
The first line of input contains an integer T denoting the number of test cases. Each test case consist of a string in 'lowercase' only in a separate line.
Output:
Print the lexicographically smaller character which occurred the maximum time.
Constraints:
1 ≤ T ≤ 30
1 ≤ |s| ≤ 100
Example:
Input:
2
testsample
geeksforgeeks
Output:
e
e
Your code
#include<iostream>

using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
char i;
int j;
int max=-1;
int l=s.length();
char c;
for(i='a'; i<='z'; i++)
{   int num=0;
for(j=0; j<l; j++)
{
if(i==s[j])
{
num++;
}
}
if(max<num)
    { max=num;
    c=i;
}
    }
    cout<<c<<endl;
   
}
}

Follow us

Follow Bijendra Kumar on Facebook