You need two structures:
= frequency of value in current range
= how many values have frequency exactly Also track = current maximum frequency. When adding value :
- Decrease
- Increase
- Increase
- Update if When removing: similar, but might decrease. If becomes , decrement .