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 33aca2b commit b2b7792Copy full SHA for b2b7792
top-k-frequent-elements/delight010.swift
@@ -0,0 +1,11 @@
1
+class Solution {
2
+ func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] {
3
+ var dictionary: [Int: Int] = [:]
4
+ for num in nums {
5
+ dictionary[num, default: 0] += 1
6
+ }
7
+
8
+ return dictionary.sorted(by: { $0.value > $1.value }).prefix(k).map(\.key)
9
10
+}
11
0 commit comments