Skip to content

Commit 12faf10

Browse files
week 1: top k elements
1 parent 124f26a commit 12faf10

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
3+
from collections import defaultdict
4+
frequent_dict = defaultdict(int)
5+
for num in nums:
6+
frequent_dict[num] += 1
7+
sorted_dict = dict(sorted(frequent_dict.items(), key=operator.itemgetter(1), reverse=True))
8+
return list(sorted_dict)[:k]

0 commit comments

Comments
 (0)