Skip to content

Commit 665b9bb

Browse files
top k
1 parent 7a2ab91 commit 665b9bb

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
3+
counter = dict()
4+
5+
for num in nums:
6+
if num in counter:
7+
counter[num] += 1
8+
else:
9+
counter[num] = 1
10+
11+
return [num[0] for num in sorted(counter.items(), key=lambda x: x[1])[-k:]]
12+

0 commit comments

Comments
 (0)