Practice · Hashing · 7 of 7

Most Frequent Values

You are given a list of integers nums and an integer k. Return the k values that appear most often in nums, in any order.

The tests are chosen so the answer is unique: each of the k values appears strictly more often than every value left out.

Example 1

Input
nums = [5, 3, 5, 9, 3, 5], k = 2
Output
[5, 3]
5 appears three times and 3 twice; 9 appears only once.

Example 2

Input
nums = [-1, -1, 4], k = 1
Output
[-1]

Constraints

  • nums holds 1 to 10⁵ numbers, each between −10⁴ and 10⁴.
  • k is between 1 and the number of different values in nums.
  • There is no tie at the boundary, so exactly one set of k values is right.