Find the Modal Number, Smallest on a Tie
Arrays & HashingAuto-graded · 25 test casesLast updated
Tests frequency counting with a hash map and a deterministic numeric tie-break, returning the value itself without altering the input list.
Find the Modal Number, Smallest on a Tie
You are given a non-empty list of integers. Determine which value occurs the greatest number of times in the list and return that value.
If more than one value shares the greatest number of occurrences, return the numerically smallest among them. The tie is decided purely by numeric value. The position at which a value first appears in the list has no influence on the answer.
The function must return the integer itself, not how many times it appears and not its position in the list. The input list must be left unchanged.
Function
def modal_smallest(nums: List[int]) -> int:
Constraints
1 <= len(nums) <= 200_000-10**9 <= nums[i] <= 10**9- The list is never empty.
- Do not modify
nums. - Return a single integer: the value itself.
Examples
Example 1
Input: modal_smallest([4, 1, 4, 2, 2])
Output: 2
Example 2
Input: modal_smallest([-1, -1, 3])
Output: -1
Hints
Hint 1
Separate the work into two phases: first find how often each distinct value occurs, then decide which value wins.
Hint 2
A dictionary (or collections.Counter) gives you the occurrence count of every value in one pass. Then look at the largest count.
Hint 3
Collect only the values whose count equals that largest count and take their minimum. Do not rely on first-seen order or on Counter.most_common, since ties there are not resolved by numeric value.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Split the problem into two simple steps.
- Count. Walk through the list once and record, in a hash map, how many times each distinct value appears.
- Select. Find the largest count. Gather every value whose count equals that largest count, and return the smallest of those values.
Why it is correct
- After the counting pass, each key maps to exactly the number of times that value occurs, because each element increments only its own entry.
- The list is non-empty, so the map is non-empty and the maximum count exists and is reached by at least one value.
- The values whose count equals the maximum are exactly the ones tied for most frequent. Taking their minimum applies the required tie rule directly.
- The result depends only on the multiset of values, not on the order of the input or on dictionary iteration order, so first-occurrence order never matters. The input is only read, never changed.
Complexity
Let n be the length of the list and k the number of distinct values.
- Time: O(n) for counting, plus O(k) to find the maximum and O(k) to filter and take the minimum, so O(n) overall.
- Space: O(k) for the counter, which is O(n) in the worst case.
Pitfalls
- Using
Counter.most_common(1): on ties it does not choose the smallest value. - Initializing a running best to
0or-1, which are legitimate input values. - Sorting the input in place, which violates the no-mutation rule (sorting a copy is fine but O(n log n)).
- Returning the count or an index instead of the value.
Reference solution
from collections import Counter
from typing import List
def modal_smallest(nums: List[int]) -> int:
counts = Counter(nums)
top = max(counts.values())
return min(v for v, c in counts.items() if c == top)