Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- """
- import heapq
- from collections import Counter
- def top_k_frequent(nums, k):
- count = Counter(nums)
- return heapq.nlargest(k, count.keys(), key=count.get)
- # Example usage
- nums = [1, 1, 1, 2, 2, 3]
- k = 2
- print(top_k_frequent(nums, k)) # Output: [1, 2]
Advertisement
Add Comment
Please, Sign In to add comment