Arrays & HashingMedium

Top K Frequent Elements

Return the k numbers that appear most often in nums, in any order. The answer is always unique.

Example 1nums = [7, 7, 7, 3, 3, 9], k = 2 → [7, 3]
Example 2nums = [4], k = 1 → [4]
1 ≤ n ≤ 10⁵ · 1 ≤ k ≤ number of different values

Hints

0 of 3 open
Hint 2 · the patternHint 3 · the key line