Top K Frequent Elements
Return the k values that appear most often in nums, ordered from most frequent to least frequent.
The tests never have a tie that would change which values make the top k or their order.
Example 1
nums [1,1,1,2,2,3]
k 2
returns [1,2]
1 appears 3 times and 2 appears twice.
Example 2
nums [4]
k 1
returns [4]
Only one value.
Constraints
1 <= len(nums) <= 501 <= k <= number of distinct values
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Hash maps problems
- Unique Morse Code WordsEasy
- Unique OccurrencesEasy
- Widest Spread ValueEasy
- Bulls and CowsMedium
- Contiguous ArrayMedium
- Group AnagramsMedium
- Longest Consecutive SequenceMedium
- Longest Zero-Sum StretchMedium