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 84d8fe6 commit 51cd76cCopy full SHA for 51cd76c
top-k-frequent-elements/radiantchoi.swift
@@ -0,0 +1,17 @@
1
+class Solution {
2
+ func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] {
3
+ var occurences = [Int: Int]()
4
+
5
+ for num in nums {
6
+ if let occurence = occurences[num] {
7
+ occurences[num] = occurence + 1
8
+ } else {
9
+ occurences[num] = 1
10
+ }
11
12
13
+ let numbers = occurences.keys.sorted { occurences[$0] ?? 0 > occurences[$1] ?? 0 }
14
15
+ return Array(numbers[0..<k])
16
17
+}
0 commit comments