Advanced Topic 49 of 52 Data Structures
HyperLogLog
Probabilistic counter estimating distinct elements.
How it works
HyperLogLog estimates the number of distinct elements in a very large stream using tiny memory. It hashes each item, uses the first bits to pick a register and records the maximum number of leading zeros seen there. A harmonic mean over registers gives the estimate, with a standard error of about 1.04 divided by the square root of the register count, so a few kilobytes handle billions of items.
Step-by-step walkthrough
- Each item is hashed to a random bit string.
- Leading zeros are counted, a long run is rare.
- Registers keep the maximum run seen.
- Many distinct items make large maxima likely.
- A harmonic average of registers gives the distinct count within a few percent using kilobytes.
Where it is used
Counting unique visitors, distinct queries in analytics, database cardinality estimation and monitoring systems use HyperLogLog, built into Redis and BigQuery. Sketches can be merged by taking register-wise maximums.
Common pitfall
Estimates are approximate, with errors of one or two percent, and small cardinalities need correction. A poor hash function destroys accuracy. It cannot enumerate members or support deletion. Do not use it when exact counts are legally or financially required.
Where it fits in the course
HyperLogLog is topic 49 of 52 in Data Structures, rated Advanced. It follows Count-Min Sketch. The next topic is Merkle Trees.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Data Structures quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Data Structures quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is HyperLogLog?
Where do people use HyperLogLog?
What is the most common mistake with HyperLogLog?
What should I learn before this topic?
How can I practice it?
Is HyperLogLog hard to learn?
How long does it take to learn HyperLogLog?
Which topics are related to HyperLogLog?
What should I study after HyperLogLog?
Related topics
- Count-Min Sketch (Data Structures)
- Merkle Trees (Data Structures)
- Cuckoo Hashing (Data Structures)
- Rope Data Structure (Data Structures)