Retrieve Minimum and Mode from Array
Arrays & HashingAuto-graded · 22 test casesLast updated
Tests efficient computation of two simple aggregate statistics using optimal data structures and complexity analysis.
You are given an array of integers. Your task is to return a list containing exactly two values:
- The smallest number in the array
- The number that appears most frequently in the array
If multiple numbers tie for the highest frequency, return the smallest among them.
Input:
nums: a list of integers (may be empty)
Output:
- A list of two integers
[minimum, mode], or an empty list if the input is empty
Constraints:
- 0 ≤ len(nums) ≤ 10⁵
- -10⁹ ≤ nums[i] ≤ 10⁹
- When multiple values share the maximum frequency, return the numerically smallest one
Examples
Example 1
Input: get_min_and_mode([1, 2, 3, 1, 2, 1])
Output: [1, 1]
Example 2
Input: get_min_and_mode([4, -1, 4, 2, -1, -1, 4])
Output: [-1, -1]
Hints
Hint 1
The minimum requires examining every element at least once—can you do better than O(n)?
Hint 2
For the mode, consider how a hash map lets you count frequencies in a single pass.
Hint 3
When breaking ties for the mode, you need both the count and the value itself.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem asks for two separate statistics: the minimum value and the mode (most frequent value). Each can be computed efficiently, but they require different techniques.
Finding the minimum:
Scan the array once, maintaining the smallest value encountered so far. This is optimal—any correct algorithm must examine every element because an unseen element could be smaller than all observed values.
- Time complexity: O(n)
- Space complexity: O(1)
No auxiliary data structure can improve this asymptotically for a single query. Python's built-in min() implements this approach.
Finding the mode:
A naive approach would count each distinct value's occurrences by scanning the entire array for each value, giving O(n²) time. We can do much better by trading memory for speed.
Use a hash map to count frequencies in a single pass. As we iterate through the array, we increment the count for each value. After one pass, the hash map contains the exact frequency of every distinct element.
To handle ties (multiple values with the same maximum frequency), we choose the smallest value. Python's Counter combined with min() using a custom key achieves this: the key (-counts[value], value) sorts first by descending frequency, then by ascending value.
- Time complexity: O(n) for counting + O(k) for finding the maximum, where k is the number of distinct values. Since k ≤ n, this is O(n) overall.
- Space complexity: O(k) for the hash map, where k ≤ n in the worst case.
Why this is correct:
The hash map approach guarantees that after processing all elements, counts[x] equals the true frequency of x. The min() call with the composite key (-counts[value], value) naturally handles tie-breaking: among all values with maximum count, it selects the smallest.
Edge cases:
- Empty array: return an empty list
- Single element: it is both the minimum and the mode
- All elements equal: that value is both statistics
- All elements distinct: every value has frequency 1, so the smallest value is the mode
Complexity
- Time: O(n)
- Space: O(k) where k is the number of distinct values
Solution
from typing import List
from collections import Counter
def get_min_and_mode(nums: List[int]) -> List[int]:
if not nums:
return []
minimum = min(nums)
counts = Counter(nums)
mode = min(counts, key=lambda value: (-counts[value], value))
return [minimum, mode]