Skip to content

Commit b2b7792

Browse files
committed
top k frequent elements
1 parent 33aca2b commit b2b7792

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)