A python solution


  • 0
    L
    class Solution(object):
        def topKFrequent(self, nums, k):
            d = {}
            for v in nums:
                d[v] = d.get(v, 0) + 1
            l = sorted(d, key=d.get, reverse=True)
            res = []
            for i in range(k):
                res.append(l[i])
            return res
    

Log in to reply
 

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