Skip to content

Commit 958eaf1

Browse files
committed
top-k-frequent-elements : still working
1 parent f398dca commit 958eaf1

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

top-k-frequent-elements/suhacs.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
2+
3+
function top_k_frequent_element(numArr, k) {
4+
let element_qty = [];
5+
const numSet = [...new Set(numArr)];
6+
for (num of numSet) {
7+
const count = numArr.filter((x) => x === num).length;
8+
element_qty.push({ [num]: count });
9+
}
10+
Object.keys(element_qty).forEach((key) => element_qty[key]);
11+
}
12+
13+
const Arr = [1, 2, 3, 4, 5, 5, 5, 5, 3, 3, 32, 2, 2, 1];
14+
top_k_frequent_element(Arr);

0 commit comments

Comments
 (0)