Problem 239958 · easy · Level 02 Linear Data Structures

Most Frequent Value

arrays · counting · dictionaries

Return the value that occurs most often in the non-empty list nums. If several values are tied for the highest count, return the smallest of them.

Examples

Input:  nums = [4, 1, 4, 1, 2]
Output: 1
Explanation: 4 and 1 both appear twice; 1 is smaller.

Input:  nums = [7, 7, 3]
Output: 7

Constraints

  • 1 <= len(nums) <= 10**5
  • -10**9 <= nums[i] <= 10**9
  • Target: O(n) time.

Goals

  • Build a frequency table in one pass
  • Break ties by a secondary key (smallest value)
Starting Python…