MCPcopy Create free account
hub / github.com/HuberTRoy/leetCode / topKFrequent

Method topKFrequent

Array/TopKFrequentElements.py:29–44  ·  view source on GitHub ↗

:type nums: List[int] :type k: int :rtype: List[int]

(self, nums, k)

Source from the content-addressed store, hash-verified

27"""
28class Solution(object):
29 def topKFrequent(self, nums, k):
30 """
31 :type nums: List[int]
32 :type k: int
33 :rtype: List[int]
34 """
35
36 nums_dict = {}
37
38 for i in nums:
39 try:
40 nums_dict[i] += 1
41 except KeyError:
42 nums_dict[i] = 1
43
44 return sorted(nums_dict, key=lambda x: nums_dict[x], reverse=True)[:k]

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected