-
Notifications
You must be signed in to change notification settings - Fork 1
/
FrequencyCounter.js
41 lines (32 loc) · 945 Bytes
/
FrequencyCounter.js
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
let arr = [2 ,3 , 4 , 2 , 5 , 3 , 6 , 1 , 1,2];
//I know two ways of finding frequency one is hashmap(like creating one obj) and second one is reduce
const hashmap = {};
let s = new Set();
arr.forEach((it) =>{
if(it in hashmap)
{
hashmap[it]++;
s.add(it);
//console.log(it);
}
else{
hashmap[it] = 1;
}
})
let arr2 = [...s];
console.log(arr2);
console.log(Array.from(s));
// console.log(hashmap);
//this is the manual way now we will use reduce which is afuncion in js
// const arr1 = [1 , 2, 1 , 1 , 4 , 5 , 2 , 4 ];
// const count = arr1.reduce((accumulator, value) => {
// accumulator[value] = ++accumulator[value] || 1;
// return accumulator;
// }, {});
// const arr2 = [1 , 2, 1 , 1 , 4 , 5 , 2 , 4 ];
// const count2 = arr2.reduce((accumulator, value) =>
// {
// accumulator[value] = ++accumulator[value] || 1;
// return accumulator;
// }, {});
// console.log(count);