educative.io

You can remove frequencyMap code from pop method?

The below method in pop() is not necessary

    if (this.frequencyMap[num] > 1) {
      this.frequencyMap[num] -= 1;
    } else {
      delete this.frequencyMap[num];
    }

Because at worst the frequency will be zero and after that the elements is no more in heap.
If it’s found again then we just do the increment += 1. No harm done

Anyone else agree? Or Am I missing something?

2 Likes

Hi @Gaurav7 ,
Yes, there won’t be any harm done if we don’t delete it, but if there are different numbers frequently removed and become 0, there will be many 0s in the output, which we want to avoid; otherwise, the solution is correct.

1 Like