Top K Frequent Elements
Given an integer array nums and an integer k, return the k most frequent elements. The answer may be returned in any order and is guaranteed to be unique.
Open official problem prompt ↗Return the k values that occur most often in the array.
Like sorting mail into pigeonholes labeled by how many letters each recipient got, then reading off the fullest pigeonholes first until you have named k recipients.
- Input
- nums = [1, 1, 1, 2, 2, 3], k = 2
- Output
- [1, 2]
- Why
- 1 occurs three times and 2 occurs twice, the two highest frequencies, so they are the top 2.
1 <= nums.length <= 10^5-10^4 <= nums[i] <= 10^41 <= k <= number of distinct elements in numsThe answer is guaranteed to be unique