medium
Top K Frequent Elements
Given an integer array and an integer k, return the k values that occur most often. The answer is guaranteed unique and should be found faster than sorting all distinct values by frequency.
Constraints
- 1 ≤ n ≤ 10^5
- 1 ≤ k ≤ number of distinct values
- -10^4 ≤ nums[i] ≤ 10^4
Examples
in: nums = [1,1,1,2,2,3], k = 2
out: [1, 2]
Code it yourself
Solve in
Test execution is not yet available for this exercise.Practice journal →Draft saved in this browser.
Hints:
Which approach applies?
Choose an approach to check your pattern recognition, or reveal the discussion when you need help.