Skip to content

Commit 782fd83

Browse files
committed
top k frequent elements
1 parent 0d38c78 commit 782fd83

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

top-k-frequent-elements/6kitty.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#
2+
# @lc app=leetcode id=347 lang=python3
3+
#
4+
# [347] Top K Frequent Elements
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
10+
sol = {}
11+
for i in nums:
12+
sol[i] = sol.get(i, 0) + 1
13+
14+
sorted_items = sorted(sol.items(), key=lambda x: x[1], reverse=True)
15+
16+
ssol = []
17+
for i in range(k):
18+
ssol.append(sorted_items[i][0])
19+
20+
return ssol
21+
22+
23+
24+
25+
26+
27+
# @lc code=end
28+

0 commit comments

Comments
 (0)