MinHash / LSH
Estimates the Jaccard similarity of two sets using only k small "signature" numbers per set, never the full sets themselves. Locality-sensitive hashing (LSH) builds on this to find similar items in huge collections without comparing every pair.
How it works
For each of k independent hash functions, a MinHash signature keeps the minimum hash value seen across every element added to the set. A classic result (Broder, 1997) shows that, under a random permutation of all possible elements, the minimum element of A ∪ B falls in A ∩ B with probability exactly |A∩B| / |A∪B|: the Jaccard similarity. So P(minhash_i(A) == minhash_i(B)) = Jaccard(A, B) for each hash function, and averaging agreement across k independent hashes gives an unbiased estimate with standard error ≈ √(J(1−J)/k).
This is what makes MinHash useful at scale: comparing two 32-number signatures is far cheaper than comparing two full sets, and you never need to bring both original sets into memory at once. LSH takes this further by splitting each signature into bands and bucketing items so that only likely-similar pairs are ever compared directly, turning an O(n²) all-pairs comparison into something close to linear.
Interactive demo
Signatures (first 24 of 32 shown)
Highlighted cells are positions where A and B's signatures agree: the fraction of agreement across all k positions is the similarity estimate above.
API surface
| Operation | Description | Complexity |
|---|---|---|
| new MinHash(k) | Allocate a length-k signature, initialized to +Infinity. | O(k) space |
| add(item): void | Update each of the k signature slots with min(current, hash_i(item)). | O(k) time |
| similarity(other): number | Fraction of signature positions that match: the Jaccard estimate. | O(k) time |
| exactJaccard(a, b): number | Ground-truth Jaccard similarity for comparison. | O(|a|+|b|) time |
References
Original papers
- On the Resemblance and Containment of DocumentsAndrei Z. Broder · 1997
- Approximate Nearest Neighbors: Towards Removing the Curse of Dimensionality (LSH)Piotr Indyk, Rajeev Motwani · 1998
Library implementations
Real-world use cases
- Near-duplicate web-page detection at web-crawl scale (the original AltaVista use case)
- LLM training-data deduplication, e.g. RedPajama-Data-v2 / SlimPajama
- Genomic similarity estimation (Mash) and plagiarism/code-clone detection
- Elasticsearch native min_hash token filter; recommendation systems clustering by set similarity