Frequency Queries

  • + 1 comment

    I have only put 1 condition in second case for if map[x] > 0 then execute the code.

    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) {
            if (map[x] > 0) { 
                map[x] = f - 1;
                freq[f - 1] += 1;
                freq[f] -= 1;
            }
        }
        if (op === 3) {
            result.push(freq[x] > 0 ? 1 : 0);
        }
    }
    return result;