Frequency Queries

  • + 3 comments

    I have almost the same code as you, but I have a few tests failing, all in the test cases with 100000 operations :( If I pastge your code directly, it works. I've optimised my code to be as close as yours, just to try and see what happens, and it doesn't work. Can you see any difference?

    function freqQuery(queries) {
        let result = [];
        let freq = [];
        let map = {};
    
        for (let i = 0; i < queries.length; i++) {
            const [op, x] = queries[i];
            const f = map[x] || 0;
    
            if (op === 1) {
                map[x] = f + 1;
                freq[f] = (freq[f] || 0) - 1;
                freq[f+1] = (freq[f+1] || 0) + 1;
            }
            if (op === 2) {
                map[x] = f - 1;
                freq[f-1] += 1;
                freq[f] -= 1;
            }
            if (op === 3) {
                result.push(freq[x] > 0 ? 1 : 0);
            }
        }
    
        return result;
    }