# Top K Frequent Elements (Leetcode 347)

Problem Link: <https://leetcode.com/problems/top-k-frequent-elements/>

```python
import heapq
class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        d={}
        for i in nums:
            if(i not in d):
                d[i]=1
            else:
                d[i]+=1
        
        pq=[]
        heapq.heapify(pq)
        for i in d:
            heapq.heappush(pq,(-d[i],i))
        
        ans=[]
        for i in range(k):
            freq,ele=heapq.heappop(pq)
            ans.append(ele)
        
        return ans
```
