Top K Frequent Elements
MediumArrays & Hashing
Decode it first. Clues, constraints, pattern and plan before any code.Given a list of integers nums and a number k, return the k values that appear most often. You can return them in any order. Aim for better than O(n log n).
nums = [1, 1, 1, 2, 2, 3], k = 2 → [1, 2]
nums = [4, 4, 4, 6, 6, 5], k = 1 → [4]
nums = [1], k = 1 → [1]Constraints
1 <= nums.length <= 10^5-10^4 <= nums[i] <= 10^41 <= k <=the number of distinct values.- The answer is unique: there is no tie at the cut-off.