Skip to content

Commit 0b11f20

Browse files
committed
top-k solution
1 parent e594a4b commit 0b11f20

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+
class Solution:
2+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
3+
value_dict = {}
4+
5+
for num in nums:
6+
if num in value_dict:
7+
value_dict[num] += 1
8+
else:
9+
value_dict[num] = 1
10+
11+
sorted_items = sorted(value_dict.items(), key=lambda x: x[1], reverse=True)
12+
13+
return [key for key, value in sorted_items[:k]]

0 commit comments

Comments
 (0)