Most Common Character

Easy ⏱ 8 min 87% acceptance ★★★★☆ 4.3
Write a function most_common_char(s) using collections.Counter that returns the single most frequently occurring character in string s (ignoring spaces). Break ties by returning the character that appears first in s.

Examples

Example 1
Input
most_common_char('mississippi')
Output
'i'
Explanation

'i' and 's' both appear 4 times; 'i' appears first in the string.

Constraints

  • Ignore space characters entirely.
  • s has at least one non-space character.

Topics

DictionariesCounter

Companies

Goldman SachsJPMorgan Chase

Hints

Hint 1

`Counter(s.replace(" ", ""))` gives counts.

Hint 2

On a tie, `Counter.most_common()` order is insertion order among equal counts, which matches first-seen order.

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