Most Frequent Values
Return the k most frequent values in nums.
Order the result by frequency (highest first). Break ties by the smaller value first.
Requirements
- Aim for better than , for example with bucket sort, although a sort-based solution is accepted.
Constraints
- number of distinct values
Examples
EXAMPLE WALKTHROUGHclear winners
A guided view of the published example, not a trace of your code.
INPUT
[[1,1,1,2,2,3],2]TASKmost_frequent_values()
EXPECTED OUTPUT
[1,2]Start with the input values shown in this example.
Example 1 of 3
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Explanation: clear winners
Input: nums = [4,4,5,5,6], k = 2
Output: [4,5]
Explanation: tie broken by value
Input: nums = [9], k = 1
Output: [9]
Explanation: single value
Hint 1
Count with a dictionary or collections.Counter.
Hint 2
Sort the (value, count) pairs by (-count, value).