We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3c1ea6a commit a309b1eCopy full SHA for a309b1e
top-k-frequent-elements/mandoolala.py
@@ -0,0 +1,15 @@
1
+from typing import List
2
+
3
+class Solution:
4
+ def topKFrequent(self, nums: List[int], k: int) -> List[int]:
5
+ countDict = {}
6
+ for num in nums:
7
+ if num in countDict:
8
+ countDict[num] += 1
9
+ else:
10
+ countDict[num] = 1
11
+ sortedDictList = sorted(countDict.items(), key=lambda item: item[1], reverse = True)
12
+ freqElements = []
13
+ for i in range(k):
14
+ freqElements.append(sortedDictList[i][0])
15
+ return freqElements
0 commit comments