Most frequent word
Given a list of words, return the word that occurs most often.
If several words are tied, return the one that first appeared earliest in the list.
Case matters: “Cat” and “cat” are different words.
If the list is empty, return None.
Constraints: up to 100,000 words; the solution must run in O(n).
Examples
- most_frequent(['cat', 'dog', 'cat']) → 'cat'
- most_frequent(['a', 'b', 'b', 'a']) → 'a'
- most_frequent(['x']) → 'x'
- most_frequent([]) → None
Show hint
You need to remember two things about each word: how many times it occurred, and where it first appeared.
Your solution
Ctrl/⌘ + Enter runs the examples, Ctrl/⌘ + Shift + Enter submits
Loading Python…