Skip to content
SpecializedSource checked

HyperLogLog

HyperLogLog organizes data around a query, update, ordering, or approximation invariant.

Quick Facts

Compact metadata for implementation and discovery.

Category
Data Structures
Difficulty
Intermediate
Family
Data Structure
Tier
Specialized
Status
Verified
Review
Source Checked
Sources
1
Formulas
1
Relationships
1
Code example
Ready
Visualization
Ready

Overview

HyperLogLog is a data structure that maintains an invariant that supports efficient queries, updates, summaries, or indexing. It trades implementation complexity for faster or more compact operations on repeated workloads.

Core Concepts

invariantquery operationupdate operationstorage layout

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O(1)

Average

O(log n)

Worst

O(n)

Space

O(n)

When to Use

  • Use HyperLogLog when repeated operations benefit from its invariant or summary structure.
  • Use it when operation mix is stable enough to justify maintaining the structure.

When Not to Use

  • Avoid when the workload is too small to justify the added invariant maintenance.
  • Avoid when updates violate assumptions behind the query guarantees.

What It Does

  • Organizes data for efficient access
  • Maintains structural invariants
  • Supports queries, inserts, deletes, or summaries

Use Cases

  • Indexing data
  • Caching
  • Fast query processing

Inputs

  • Items, keys, or graph records
  • Queries or updates

Outputs

  • Stored structure
  • Query result
  • Updated structure

Key Formulas (1)

Cardinality estimate

estimate = αm · m2 / ∑(2−registeri)
estimate = alpha_m * m^2 / sum(2^-register_i)

Estimates distinct count from the harmonic mean of register values.

Pseudocode

  1. 1.Initialize the structure and invariants
  2. 2.Apply insert, query, update, or delete operations
  3. 3.Rebalance, hash, summarize, or split nodes as needed
  4. 4.Return the query result or updated structure

Canonical Example

Input
operations = insert A, insert B, query A
Output
updated structure and query result
Explanation
HyperLogLog maintains its invariant while answering the requested operation.

Implementation Notes

  • Write invariant checks for debug builds.
  • Benchmark the expected query/update mix instead of isolated operations only.

Common Pitfalls

  • Breaking the invariant during an update can make later queries silently wrong.
  • Ignoring adversarial or skewed input can erase expected performance.

Interactive Mechanism Walkthrough

Graph Frontier & Edge Relaxation

Show the structure changing as items are inserted, queried, or removed.

HyperLogLog mechanism walkthrough step 1 of 4suvwxt
Step 1 of 4: Initialize the structure and invariants
data-structure

Notes

HyperLogLog operates in O(log n) time and O(n) space within the Data Structure family. Key governing relation: estimate = alpha_m * m^2 / sum(2^-register_i). Often compared with Count-Min Sketch and Bloom Filter.

Sources (1)

What each reference supports for this entry.

HyperLogLog

Paper

Original reference for HyperLogLog cardinality estimation, stochastic averaging, and register-based estimates.

Open source

Explore Next

Good next jumps from the relationship graph, ranked for useful discovery.

Alternative toCount-Min Sketch

HyperLogLog organizes data with different query and update tradeoffs than Count-Min Sketch.

Tags

Data Structure