Data Structures19 sections · 729 units
Open in Course

Range Frequency Solution

Implement your solution

For this problem, the hash map approach is sufficient and simpler.

However, if you needed to extend to queries like "count values in range [a,b][a, b]" or "find kk-th smallest in range," the wavelet tree becomes necessary.

Implementation notes:

  • Use a dictionary mapping values to sorted lists of indices
  • Binary search to count indices in [l,r][l, r]
  • Handle missing values gracefully

Time: O(n)O(n) preprocessing, O(logn)O(\log n) per query. Space: O(n)O(n).