-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathTopKFrequentElements
43 lines (35 loc) · 1.33 KB
/
TopKFrequentElements
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Author: Sayed Mahmudul Alam
// Source: LeetCode-347: https://leetcode.com/problems/top-k-frequent-elements/description/
// Solution Ref: https://www.youtube.com/watch?v=YPTqKIgVk-k&ab_channel=NeetCode
// Example Input: nums = [1,1,1,2,2,3], k = 2
// Output: [1,2]
class Solution {
func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] {
// Dictionary to hold count of each element
var count = [Int: Int]()
// Frequency table to hold the frequency of each number
var freq = [[Int]](repeating: [], count: nums.count + 1)
// Array to hold the result
var result = [Int]()
// Populating the `count` dictionary with actual counts
nums.forEach { n in
count[n] = 1 + (count[n] ?? 0)
}
// Populate the `freq` table with elements for each number
for (_,value) in count.enumerated() {
freq[value.1].append(value.0)
}
// Loop through `freq` in reverse and add the element of inner array to `result`
// till `result` is equal `k`
let sequence = stride(from: freq.count - 1, to: 0, by: -1)
for i in sequence {
for j in freq[i] {
result.append(j)
if result.count == k {
return result
}
}
}
return []
}
}