Problem Solving/LeetCode

[Leetcode] 347. Top K Frequent Elements

yuseon-Lim 2021. 1. 26. 09:50

Leetcode 347. Top K Frequent Elements

https://leetcode.com/problems/top-k-frequent-elements/

 

Top K Frequent Elements - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

Solution 1

collections.Counter() 이용한 풀이. most_common()메소드를 사용했다.

import collections
from typing import List

class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        answer = []
        count = collections.Counter(nums)

        for tuple in count.most_common(k):
            answer.append(tuple[0])

        return answer

반응형