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.
2 parents 775606f + f8fe611 commit 317d899Copy full SHA for 317d899
dart/0347-top-k-frequent-elements.dart
@@ -1,3 +1,31 @@
1
+class Solution {
2
+ List<int> topKFrequent(List<int> nums, int k) {
3
+ Map<int, int> frequencyMap = {};
4
+ for (int num in nums) {
5
+ frequencyMap[num] = (frequencyMap[num] ?? 0) + 1;
6
+ }
7
+
8
+ List<List<int>> frequencyList =
9
+ List.filled(nums.length + 1, List<int>.empty(growable: true));
10
11
+ frequencyMap.forEach((num, count) {
12
+ frequencyList[count] = [...frequencyList[count], num];
13
+ });
14
15
+ List<int> res = [];
16
17
+ for (var i = frequencyList.length - 1; i >= 0; i--) {
18
+ res.addAll(frequencyList[i]);
19
20
+ if (res.length == k) {
21
+ break;
22
23
24
25
+ return res;
26
27
+}
28
29
class Solution {
30
List<int> topKFrequent(List<int> nums, int k) {
31
Map<int, int> map = {};
0 commit comments