-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfreqSelector.js
More file actions
50 lines (41 loc) · 1.2 KB
/
freqSelector.js
File metadata and controls
50 lines (41 loc) · 1.2 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const assert = require("assert").strict;
function freqSelector(ar, N) {
/** Returns the top N frequently used values in the array
@param {array} ar - containing all elements
@params {int} N - number of top used values to return
@returns {Array} An array of the top used values from the input array.
*/
counts = {};
for (item of ar) {
counts[item] = counts[item] ? counts[item] + 1 : 1;
}
sortedKeys = Object.keys(counts)
.sort((a, b) => counts[b] - counts[a])
.map((v) => new Object({ key: v, freq: counts[v] }));
// get only top N elements from sortedKeys, unless sortedKeys is smaller than N, then just gets the entire array
if (sortedKeys.length < N) {
return sortedKeys;
}
return sortedKeys.slice(0, N);
}
function runTests() {
tests = [
{
ar: ["a", "a", "b", "c", "d", "a", "d", "e", "f"],
N: 2,
ans: [
{ key: "a", freq: 3 },
{ key: "d", freq: 2 },
],
},
];
function testFreqSelector({ ar, N, ans }) {
topFreq = freqSelector(ar, N);
assert.deepStrictEqual(topFreq, ans, "The two arrays don't match");
}
for (test of tests) {
testFreqSelector(test);
}
}
runTests();
module.exports = freqSelector;