KatInterview

Find Minimum and Mode of an Integer Array

Arrays & HashingAuto-graded · 21 test cases
EasyAuto-gradedArrays & Hashingarrayshash-tablefrequency-counting

Last updated

Find the minimum element and the most frequent element (mode) in an unsorted list of integers.

Given a list of integers a, calculate two values:

  1. The minimum value present in a.
  2. The mode (the most frequently occurring element) in a.

If the list is empty, return [None, None].

If multiple distinct numbers tie for the highest frequency, any of those numbers is an acceptable mode.

Constraints

  • $0 \le \text{len}(a) \le 10^5$
  • $-10^9 \le a[i] \le 10^9$ for each element $a[i]$

Output Format

Return a 2-element list [min_value, mode_value]. If a is empty, return [None, None].

Examples

Example 1

Input: minimum_and_mode([1, 2, 2, 3, 3, 3, 4])

Output: [1, 3]

Example 2

Input: minimum_and_mode([-5, -1, -5, 2, -1, -5])

Output: [-5, -5]

Example 3

Input: minimum_and_mode([7])

Output: [7, 7]

Hints

Hint 1

Handle the empty array base case first by returning `[None, None]`.

Hint 2

The minimum value can be found in $O(N)$ time with a single pass or built-in function.

Hint 3

To find the mode in $O(N)$ expected time, count element frequencies using a standard hash map or dictionary, then find the element with the highest count.