Skip to content

Commit 0221937

Browse files
committed
top-k-frequent-elements: use Counter
1 parent c4b46ab commit 0221937

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

top-k-frequent-elements/haklee.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
리스트의 앞 k개의 아이템 리턴.
1616
"""
1717

18+
from collections import Counter
19+
1820

1921
class Solution:
2022
def topKFrequent(self, nums: List[int], n: int) -> List[int]:
21-
d = {}
22-
[d.update({i: d.get(i, 0) + 1}) for i in nums]
23-
return [i for _, i in sorted([(-v, k) for k, v in d.items()])][:n]
23+
return [i for _, i in sorted([(-v, k) for k, v in Counter(nums).items()])][:n]

0 commit comments

Comments
 (0)