Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements.
Example
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Solution
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
c = collections.Counter(nums)
return heapq.nlargest(k, c, c.get)