easy +10 pts

Mode of a list via counting

Find the most frequent element in a list, with ties resolved by first appearance.

Write a function `mode(lst)` that takes a list `lst` of hashable elements and returns the mode—the element that appears most frequently. If there is a tie for the highest frequency, return the element that appears first in the original list. If the list is empty, return `None`. The function signature is: `def mode(lst) -> Optional[Any]:` (You may import `Optional` and `Any` from `typing` if needed.) **Assumptions:** - The list may contain any hashable elements (e.g., integers, strings, tuples). - The list is not necessarily sorted. Your solution must be deterministic and follow the tie-breaking rule exactly.

Constraints

0 <= len(lst) <= 10^5 Elements are hashable. Time complexity: O(n), where n is the length of the list. Space complexity: O(n) for counting frequencies.

Example

```python
>>> mode([1, 2, 2, 3])
2
>>> mode([1, 1, 2, 2])
1
>>> mode([]) is None
True
>>> mode(['a', 'b', 'b', 'c', 'a'])
'a'
```
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Count frequencies with a dictionary, then scan the list in order to find the first element with the max count.
You can compute the max count first, then iterate through the original list to find the first element that has that count.
For empty input, the function should return None.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.