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 0c700f0 commit 8acdf4bCopy full SHA for 8acdf4b
top-k-frequent-elements/doh6077.py
@@ -0,0 +1,12 @@
1
+class Solution:
2
+ # dictionary use
3
+ def topKFrequent(self, nums: List[int], k: int) -> List[int]:
4
+ result = {} # key: 원소, value: 등장 횟수
5
+ for n in nums:
6
+ if n in result:
7
+ result[n] = result[n] + 1
8
+ else:
9
+ result[n] = 1
10
+
11
+ # 가장 자주 등장한 원소 k개 반환
12
+ return sorted(result.keys(), key=lambda x: result[x], reverse=True)[:k]
0 commit comments