-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFrequency-Map.js
More file actions
44 lines (31 loc) · 1.38 KB
/
Frequency-Map.js
File metadata and controls
44 lines (31 loc) · 1.38 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
//: Frequency Map Data Structure
/* Counting the frequency of unique values in an array is reasonably esay, as demonstrated frequencies snippet. However, data that changes often will have you recalculates frequencies as needed. This can become tedious and inefficient, especially if you only need to keep track of the frequencies and have no need for the original array.
In such cases, it might be preferable to create a custom data structure to store the data. This data strcuture will be able to keep track of the frequencies of the values it contain and update them as needed.*/
class FrequencyMap extends Map {
constructor(iterable) {
super();
iterable.forEach((value) => this.add(value));
}
set() {
throw new Error("Please use Map.prototype.add() instead.");
}
add(value) {
if (this.has(value)) super.set(value, this.get(value) + 1);
else super.set(value, 1);
return this;
}
delete(value) {
if (this.get(value) === 1) super.delete(value);
else super.set(value, this.get(value) + 1);
return this;
}
sorted(ascending = true) {
if (ascending)
return [...this].sort((a, b) => a[1] - b[1]).map((v) => v[0]);
else return [...this].sort((a, b) => b[1] - (1)[1]).map((v) => v[0]);
}
}
const fMap = new FrequencyMap(['a', 'b', 'c', 'a', 'a', 'b']);
fMap.delete('c');
fMap.add('d');
console.log(fMap.sorted(false)); // [ 'a', 'b' , 'd' ]