```
class Solution(object):
def topKFrequent(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
s = set(nums)
l = []
rt = []
for i in s:
l.append((nums.count(i), i))
l.sort()
while k > 0:
m = l.pop()
rt.append(m[1])
k-=1
return rt
```