This is the problem:
You are given queries. Each query is of the form two integers described below: - 1: Insert x in your data structure. - 2: Delete one occurrence of y from your data structure, if present. - 3: 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 query of size q where queries[i][0] contains the operation, and queriesi contains the data element contains the operation and contains the data element. For example, you are given array. [[1,1],[2,2],[3,2],[1,1],[1,1],[2,1],[3,2]] The output=[0,1]
The question can be found here; https://www.hackerrank.com/challenges/frequency-queries/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=dictionaries-hashmaps
My code does not work some of the cases on hackerrank for some reason but I can't figure out why. It is not caused by timeout or compiler error. It gives a 'wrong answer' in the compiler but when I try to test my code with the same input on VSCode, it actually works. It's the first time I am encountering something like this. If I would be more specific, I tested my code manually against this input
[1, 3], [1, 38], [2, 1], [1, 16], [2, 1], [2, 2], [1, 64], [1, 84], [3, 1], [1, 100], [1, 10], [2, 2], [2, 1], [1, 67], [2, 2], [3, 1], [1, 99], [1, 32], [1, 58], [3, 2]
and it returns [1,1,0] on vs code when I try it. However, on hackerrank it says the wrong answer even though it expects the same result. Any idea what's going on?
Here is my code in javascript;
function freqQuery(queries) {
let store = [];
let output = [];
let obj = {};
let checkFreq = false;
for (let i = 0; i < queries.length; i++) {
let query = queries[i];
if (query[0] === 1) {
store.push(query[1]);
} else if (query[0] === 2) {
let index = store.indexOf(query[1]);
if (index > -1) {
store.splice(index, 1);
}
} else {
let freq = query[1];
if (store.length === 0) {
output.push(0);
} else {
obj = charToObj(store);
for (let number in obj) {
if (obj[number] === freq) {
checkFreq = true;
break;
} else {
checkFreq = false;
}
}
if (checkFreq) {
output.push(1);
} else {
output.push(0);
}
}
}
}
return output;
}
function charToObj(arr) {
let obj = {};
for (let i = 0; i < arr.length; i++) {
if (obj[arr[i]]) {
obj[arr[i]] += 1;
} else {
obj[arr[i]] = 1;
}
}
return obj;
}