Top K Frequent Elements
Return the k distinct values with highest frequencies in nums, in any order.
Example 1
nums = [4,4,4,2,2,9]
k = 2[4,2]Explanation
4 occurs three times and 2 twice, exceeding the single occurrence of 9.
Example 2
nums = [-1,-1,2]
k = 1[-1]Explanation
-1 occurs twice, so it beats 2 regardless of numeric ordering.
Example 3
nums = [8]
k = 1[8]Explanation
There is only one distinct value and k is one, so return 8.
Constraints
- 1 <= k <= number of distinct values <= 100000
- The frequency boundary at k has no tie
Function contract
topKFrequentArguments: json, jsonSolutionRuns on your device
Loading draft…
Run: 3 examples; Submit: 35 cases, on your device.
[4,4,4,2,2,9]2