Python solution using a dictionary

  • 0


    def topKFrequent(self, nums, k):
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        counts_of_nums = {}
        for i in nums:
            if not i in counts_of_nums:
                counts_of_nums[i] = 1
                counts_of_nums[i] += 1
        return sorted(counts_of_nums, key=counts_of_nums.get, reverse=True)[:k]


Log in to reply

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