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 30f5aae commit 003b091Copy full SHA for 003b091
top-k-frequent-elements/doitduri.swift
@@ -0,0 +1,18 @@
1
+class Solution {
2
+ func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] {
3
+ var tables: [Int: Int] = [:]
4
+
5
+ for num in nums {
6
+ tables[num] = (tables[num] ?? 0) + 1
7
+ }
8
9
+ let values = tables.values.sorted().reversed().prefix(k) // k개의 frequent
10
+ let result = tables.compactMap { (key: Int, value: Int) -> Int? in
11
+ if values.contains(value) {
12
+ return key
13
14
+ return nil
15
16
+ return result
17
18
+}
0 commit comments