Error
403 - Forbidden

Most Frequent Element

Easy
0/40
profile
Contributed by
7 upvotes
Asked in companies
OLX GroupSAP LabsWalmart

Problem statement

Given an alphabetical string ‘S’. Return the most frequent character of ‘S’. If there are multiple characters with the same frequency equal to the maximal frequency, return the lexicographically smallest character with maximal frequency.

For example:
‘S’ = brutegroot
The most frequent characters are [o, r, t]. All of these characters appear 2 times each.
As among all the options, ‘o’ is lexicographically smallest. So the answer is ‘o’.
Hence output will be o.
Detailed explanation ( Input/output format, Notes, Images )

Input Format:

The first line of the input contains a single integer ‘T’ representing the no. of test cases.

The first line of each test case contains a single alphabetical string, ‘S’.

Output Format:

For each test case, print a single character representing the most frequent character of the given string.

Print a separate line for each test case.
Note:
You are not required to print anything; it has already been taken care of. Just implement the function and return the answer.

Constraints:

1 ≤ T ≤ 1000
1 ≤ |S| ≤ 10^5
S consists of only lowercase english alphabets.
Σ|S| ≤ 2 * 10^6

Time limit: 1 Sec
Sample Input 1 :
2
brutegroot
codingninjas
Sample Output 1 :
o
n
Explanation For Sample Input 1 :
For First Case - Same as explained in above example.

For the second case -

‘S’ = codingninjas
‘n’ appears the most number of times, i.e., 3. So the answer will be ‘n’.
Hence output will be n.
Sample Input 2 :
2
most
frequent
Sample Output 2 :
m
e
Hint
Approaches (1)
Code Solution
(100% EXP penalty)
Most Frequent Element
Full screen
Console