NumPy allowed · plain Python earns 10 bonus points
Count element frequencies and retrieve the top k keys in O(n log k) or O(n) time via bucket sorting or heaps.
Implement top_k_frequent(nums, k): return the k most frequent elements in nums. Order the returned list by frequency descending, breaking frequency ties by value ascending.
top_k_frequent(nums: list[int], k: int) -> list[int]