Data Structures · Visualization

HyperLogLog: Introduction

HyperLogLog

HyperLogLog counts DISTINCT items — how many different users visited — in about 12 KB no matter the scale. Time: O(1) per item — one hash. Space: O(m) small registers (~12 KB standard).

Big-O: O(1) per item; ~12 KB for billions of distinct.

HyperLogLog: Introduction

HyperLogLog

HyperLogLog counts DISTINCT items — how many different users visited — in about 12 KB no matter the scale. It hashes each item, uses the first bits to pick one of m registers, and stores the longest run of leading zeroes seen there. Rare long zero-runs imply many distinct items: twice as many items make the longest run one bit longer on average.

How HyperLogLog: Introduction Works

  1. Hash every item to a fixed bit-string.
  2. First p bits choose a register (m = 2^p of them).
  3. Count leading zeroes in the rest; keep the maximum per register.
  4. Combine all registers with a harmonic mean → distinct estimate.

HyperLogLog: Introduction Worked Example

Input: 4 registers; observe 'amy', 'bob', 'amy', 'cid'

  1. 'amy' → register 1, zeroes 2 → reg1 = 2.
  2. 'bob' → register 3, zeroes 1 → reg3 = 1.
  3. 'amy' again → same register, same zeroes → no change (duplicates free!).
  4. 'cid' → register 0, zeroes 3 → reg0 = 3.

Output: Registers [3, 2, 0, 1] → estimate ≈ 4 distinct (truth 3)

📖 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.
HyperLogLog: Introduction
Distinct counting in KBs: longest zero-runs imply scale.
0
r0
0
r1
0
r2
0
r3
0
r4
0
r5
0
r6
0
r7
Updated register Max zero-run seen
HyperLogLog: Introduction
Press ▶ Run to watch it step by step.
Python · HyperLogLog in Code
def add(regs, x, p=14):
    h = hash64(x)
    r = h >> (64 - p)            # register index
    z = leading_zeroes(h) + 1
    regs[r] = max(regs[r], z)    # duplicates: no-op
    # O(1): one hash
# example: intro

HyperLogLog: Introduction Time & Space Complexity

TimeO(1) per item — one hash
SpaceO(m) small registers (~12 KB standard)

When to Use HyperLogLog: Introduction

  • Daily active users and unique visitors at web scale
  • Redis PFADD / BigQuery APPROX_COUNT_DISTINCT under the hood

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.