Fastest Python Solution


  • 0
    M
    class Solution(object):
        def topKFrequent(self, nums, k):
            """
            :type nums: List[int]
            :type k: int
            :rtype: List[int]
            """
            d = {}
            for num in nums:
                d[num] = d.get(num, 0) + 1
            
            return sorted(d.keys(), key=lambda x: d[x], reverse=True)[:k]
    

Log in to reply
 

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