Given an integer array nums and an integer k, return the k values that occur most frequently in nums.
Order the result by frequency, most frequent first. If two values occur the same number of times, the larger value comes first. This ordering makes the answer unique.
Examples
Example 1
Input: nums = [1,2,2,3,3,3], k = 2
Output: [3,2]
Explanation: 3 occurs three times and 2 occurs twice, the two highest frequencies.
Example 2
Input: nums = [4,4,7,7,1], k = 2
Output: [7,4]
Explanation: 4 and 7 both occur twice. They tie on frequency, so the larger value 7 comes first.
Example 3
Input: nums = [5], k = 1
Output: [5]
Constraints
1≤nums.length≤105
−104≤nums[i]≤104
1≤k≤ the number of distinct values in nums.
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up