Data Structures · Visualization
Count-Min Sketch: Add
Count-Min Sketch
Adding touches exactly one counter per row — d increments total, no matter how many distinct items exist. Time: O(d) hashes per add. Space: O(1) extra.
Big-O: Add/estimate in O(d) hashes; tiny fixed memory.
Count-Min Sketch: Add
Count-Min SketchAdding touches exactly one counter per row — d increments total, no matter how many distinct items exist. That fixed cost is the whole point: a million-item stream costs the same per event as a ten-item one. Try your own words below and watch every row tick up together.
How Add Works
- Hash the item with each of the d hash functions.
- Increment those d counters by 1 (or by the event weight).
- Different items sharing a cell simply add up — collisions merge.
- No per-item bookkeeping is ever stored.
Add Worked Example
Input: Add 'berry' to a sketch holding apple×2, mango×1
- 'berry' hashes to (row0:4, row1:3).
- Bump row0/col4 → 1; row1/col3 was 2 (apple) → 3.
- Apple's row1 counter is now shared — its estimate may inflate.
- Mango untouched: its private cells stay exact.
Output: 2 counters bumped; total work O(d) = 2 hashes
Press ▶ Run to watch it step by step.
def add(sketch, x, w=1):
for r in range(D):
sketch[r][h(r, x)] += w # one cell per row
grid = [[0] * W for _ in range(D)] # tiny, fixed
# example: addBehind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Add Time & Space Complexity
When to Use Add
- Counting URL hits across a web fleet in fixed memory
- Feature-frequency sketches feeding online ML models
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.