Just 1-line Python solution


  • 0
    C
    from collections import Counter
    
    class Solution(object):
        def topKFrequent(self, nums, k):
            """
            :type nums: List[int]
            :type k: int
            :rtype: List[int]
            """
            return [key for value, key in sorted([(value, key) for (key, value) in Counter(nums).items()], reverse=True)[:k]]
    

Log in to reply
 

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