Top K Frequent Elements
Given an integer array nums and an integer k, return the k values that occur most often in nums.
The tests are chosen so the answer is unique: there is never a tie for the last place in the top k. You may return the values in any order.
Try to beat O(n log n), the cost of sorting all values by frequency.
Constraints
1 <= nums.length <= 10^5-10^4 <= nums[i] <= 10^41 <= k <=the number of distinct values innums- The set of the
kmost frequent values is unique.
Examples
Example 1
Input: nums = [4,4,4,6,6,9], k = 2 Output: [4,6]
4 appears three times and 6 twice, more often than 9 (once). Returning [6, 4] is just as good.
Example 2
Input: nums = [7], k = 1 Output: [7]
The only value is the most frequent one.
Example 3
Input: nums = [5,3,5,3,2,5,1,3,5], k = 1 Output: [5]
5 appears 4 times, 3 appears 3 times, 2 and 1 once each.
JavaScript you'll use: Map and Set Heaps and priority queues
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
function topKFrequent(nums, k) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.