-
Notifications
You must be signed in to change notification settings - Fork 0
/
frequency-queries.js
60 lines (49 loc) · 1.61 KB
/
frequency-queries.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
You are given queries. Each query is of the form two integers described below:
- : Insert x in your data structure.
- : Delete one occurence of y from your data structure, if present.
- : Check if any integer is present whose frequency is exactly . If yes, print 1 else 0.
The queries are given in the form of a 2-D array of size where contains the operation, and
contains the data element. For example, you are given array . The results of each operation are:
Sample Input 1
4
3 4
2 1003
1 16
3 1
Sample Output 1
0
1
Explanation 1
For the first query of type , there is no integer of frequency . The answer is . For the second query of type ,
there is one integer, of frequency so the answer is .
**/
// Complete the freqQuery function below.
function freqQuery(queries) {
const freqHash = {};
const output = [];
queries.forEach(([op, val]) => {
switch(op) {
case 1:
freqHash[val] ? freqHash[val] += 1 : freqHash[val] = 1;
break;
case 2:
if (freqHash[val]) {
freqHash[val]--;
if (freqHash[val] < 0) delete freqHash[val];
}
break;
case 3:
let freqFound = 0;
for (let key in freqHash) {
if (freqHash[key] === val) {
freqFound = 1;
break;
}
}
output.push(freqFound);
break;
}
});
return output;
}