Skip to content

Commit 004be14

Browse files
committed
Top K Frequent
1 parent 99826d9 commit 004be14

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution(object):
2+
def topKFrequent(self, nums, k):
3+
count = {}
4+
result = []
5+
6+
for i in nums:
7+
if i not in count:
8+
count[i] = 1
9+
else:
10+
count[i] += 1
11+
12+
count = sorted(count.items(), key=lambda x : x[1], reverse=True)
13+
14+
return [item[0] for item in count[:k]]
15+

0 commit comments

Comments
 (0)