Data Structures · Visualization

Count-Min Sketch: Introduction

Count-Min Sketch

A Count-Min sketch counts how often items appear using a tiny grid of counters instead of a giant dictionary. Time: O(d) per add/estimate — a few hashes. Space: O(d × w) counters, far below a dictionary.

Big-O: Add/estimate in O(d) hashes; tiny fixed memory.

Count-Min Sketch: Introduction

Count-Min Sketch

A Count-Min sketch counts how often items appear using a tiny grid of counters instead of a giant dictionary. Each item is hashed once per row and every one of those counters is bumped. To estimate, take the minimum across the rows — collisions only ever inflate counts, so the minimum is the closest to the truth.

How Count-Min Sketch: Introduction Works

  1. Fix a grid: d rows (hash functions) × w columns (counters).
  2. Add(x): for each row, counter[row][hash_row(x)] += 1.
  3. Estimate(x): return the smallest of its d counters.
  4. Width controls accuracy, depth controls confidence — both tiny.

Count-Min Sketch: Introduction Worked Example

Input: Grid 2 rows × 5 cols; add 'apple' twice, 'mango' once

  1. 'apple' hashes to (row0:1, row1:3) → bump both, twice.
  2. 'mango' hashes to (row0:1, row1:0) → shares row0/col1 with apple.
  3. Estimate 'apple': min(3, 2) = 2 ✓ exact.
  4. Estimate 'mango': min(3, 1) = 1 ✓ (row0 inflated, row1 saves it).

Output: apple ≈ 2, mango ≈ 1 — never an underestimate

📖 Introduction: this is an overview of the topic, so there's nothing to run here. Pick an operation from the side menu to see a live, step-by-step animation.
Count-Min: Introduction
A tiny counter grid estimating frequencies — never underestimates.
row 0
0
0
0
0
0
0
0
0
row 1
0
0
0
0
0
0
0
0
row 2
0
0
0
0
0
0
0
0
Touched cell Non-zero counter
Count-Min: Introduction
Press ▶ Run to watch it step by step.
Python · Count-Min Sketch in Code
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: intro

Count-Min Sketch: Introduction Time & Space Complexity

TimeO(d) per add/estimate — a few hashes
SpaceO(d × w) counters, far below a dictionary

When to Use Count-Min Sketch: Introduction

  • Heavy-hitter detection in network traffic and ad clicks
  • Stream frequency estimates where storing everything is impossible

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.