Skip to content

Commit f39db86

Browse files
committed
adding top k frequent element
1 parent 14fe212 commit f39db86

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
3+
counts = Counter(nums)
4+
5+
items = counts.items()
6+
7+
sort_by_value = sorted(
8+
items,
9+
key=lambda item: item[1],
10+
reverse = True
11+
)
12+
result = []
13+
for i in range(k):
14+
result.append(sort_by_value[i][0])
15+
return result
16+

0 commit comments

Comments
 (0)