Data Structures19 sections · 729 units
Open in Course

Problem - Mode Query

Most frequent element

Given an array of nn integers and qq queries, each query asks: what is the most frequent value in range [l,r][l, r]? If there are ties, return any. Constraints: n,q105n, q \leq 10^5.

This is trickier than distinct values. Adding an element might change the mode. Removing an element might also change it.

Think about what data structures you need to track both the frequency of each value and which value has the highest frequency. Can you do add/remove in O(1)O(1)?