[LeetCode] 347. Top K Frequent Elements (Python)
- Reference
- 카운터 쓰면 매우 쉽게 풀리는 문제
Solution
import collections
from typing import List
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
answer = []
counter = collections.Counter(nums)
tmp = counter.most_common(k)
for t in tmp:
answer.append(t[0])
return answer
Leave a comment