-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path347.cs
More file actions
34 lines (29 loc) · 1.03 KB
/
Copy path347.cs
File metadata and controls
34 lines (29 loc) · 1.03 KB
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
public class Solution {
public int[] TopKFrequent(int[] nums, int k) {
var dictionaryOfNums = new Dictionary<int, int>();
List<int>[] dictionaryOfReps = new List<int>[nums.Length + 1];
foreach (var num in nums){
if (!dictionaryOfNums.ContainsKey(num)){
dictionaryOfNums.Add(num, 1);
}
else{
dictionaryOfNums[num]++;
}
}
for (int i = 0; i < nums.Length + 1; i++) {
dictionaryOfReps[i] = new List<int>();
}
foreach(var key in dictionaryOfNums.Keys){
dictionaryOfReps[dictionaryOfNums[key]].Add(key);
}
var resultList = new List<int>();
for (int i = dictionaryOfReps.Length - 1 ; i >= 0 ; i--){
for (int j = 0; j < dictionaryOfReps[i].Count; j++){
if(resultList.Count != k ){
resultList.Add(dictionaryOfReps[i][j]);
}
}
}
return resultList.ToArray();
}
}