```
class Solution(object):
def topKFrequent(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
l = len(nums)
counts=dict()
g = []
lst = []
for n in nums:
counts[n] = counts.get(n,0)+1
for key, val in counts.items():
lst.append((val,key))
lst.sort(reverse=True)
l1 = len(lst)
for i in range(k):
g.append(lst[i][1])
return g
```