Most Frequent Character

Medium ⏱ 12 min 45% acceptance ★★★★★ 4.6
Write most_frequent_char(s) that returns the character (ignoring case and spaces) that appears most often in s. If several characters tie for the highest count, return whichever one reaches that count first while scanning left to right.

Examples

Example 1
Input
s = 'programming'
Output
'r'
Explanation

'r' and 'g' and 'm' all appear twice, but 'r' is the first to reach a count of 2 while scanning left to right.

Example 2
Input
s = 'aabbbcc'
Output
'b'
Explanation

'b' appears 3 times, more than any other character.

Constraints

  • s has at least one non-space character.

Topics

StringsCharacter Frequency

Companies

NetflixAdobe

Hints

Hint 1

Build a frequency dictionary in one pass.

Hint 2

Then scan the string again, tracking the best count seen so far and only updating on a strictly higher count.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.