Concise Python

  • 0

    I'm working my way through Fluent Python. It talks about the collection.Counter object, which was well suited for this problem.

    from collections import Counter
    class Solution(object):
        def topKFrequent(self, nums, k):
            :type nums: List[int]
            :type k: int
            :rtype: List[int]
            counts = Counter(nums)
            return [i[0] for i in counts.most_common(k)]

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.