Skip to content

Commit 9131a4b

Browse files
committed
Week 01: top-k-frequent-elements solution
1 parent 0a3b42a commit 9131a4b

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import List
2+
3+
class Solution:
4+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
5+
nums_dict = {}
6+
for n in nums:
7+
if n not in nums_dict.keys():
8+
nums_dict[n] = 1
9+
else:
10+
nums_dict[n] += 1
11+
12+
frequent_rank = sorted(nums_dict.items(), key=lambda item:item[1], reverse=True)
13+
return [frequent_rank[j][0] for j in range(k)]

0 commit comments

Comments
 (0)