๐Ÿ“– Top K Frequent Elements

๐Ÿ“–What I thought

  • ๋ฆฌ์ŠคํŠธ ๊ฐ’์˜ ๋นˆ๋„์ˆ˜ โ†’ Counter ์“ฐ๋ฉด ๋˜์ž–์•„~
  • Counter ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด์„œ most_common()ํ•จ์ˆ˜ ์“ฐ๊น…

๐Ÿ“–ํ’€์ด

๐Ÿ“ŽCounter

from collections import Counter
 
class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        freq = Counter(nums).most_common(k)
 
        return [v for v, cnt in freq]
  • most_common()ํ•จ์ˆ˜๋ฅผ ์“ฐ๋ฉด (์š”์†Œ, ๋นˆ๋„์ˆ˜)์˜ ํ˜•ํƒœ๋กœ ๋ฆฌ์ŠคํŠธ์— ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์ˆœ์œผ๋กœ ์ €์žฅ

๐Ÿ“–What I learned

๐Ÿ“–๊ด€๋ จ ์ง€์‹

๐Ÿ“ŽCounter