Fast approximate algorithm for cardinality of sets intersection
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Computing the exact size of a set intersection can be expensive when the sets are huge, distributed, or streaming. In those cases, an approximate algorithm is often good enough, especially if you need a fast answer for similarity search, deduplication, telemetry, or large-scale analytics.
A Practical Strategy: Estimate Jaccard, Then Recover Intersection Size
One useful approach is to estimate the Jaccard similarity between two sets and then turn that estimate into an intersection estimate.
The Jaccard similarity is:
If you also know or can estimate |A| and |B|, then you can estimate the intersection:
MinHash is a classic way to estimate Jaccard similarity efficiently.
Example MinHash Sketch
This small Python example builds a simple MinHash signature and uses it to estimate intersection size:
This technique is useful when you can afford a sketch per set but not an exact pairwise intersection scan for every comparison.
When HyperLogLog Helps
If the set sizes themselves are not known exactly, combine a Jaccard estimator such as MinHash with a distinct-count sketch such as HyperLogLog. HyperLogLog is strong at approximate cardinality estimation, but it does not directly give you intersection size by itself.
A common pattern is:
- Use HyperLogLog for
|A|and|B| - Use MinHash for Jaccard similarity
- Derive
|A ∩ B|from those estimates
That combination is often far more practical than exact set materialization in large systems.
Why Approximation Is Worth It
Approximate algorithms help when:
- The sets are too large to intersect exactly in memory
- The data arrives as streams
- You need many repeated similarity checks
- Network cost matters more than exactness
They trade a controlled amount of error for big gains in speed and memory efficiency.
Common Pitfalls
The biggest mistake is choosing an approximate algorithm without understanding what quantity it estimates. MinHash estimates Jaccard similarity, not intersection size directly. HyperLogLog estimates cardinality, not similarity.
Another issue is using sketches that are too small. Tiny signatures or low-register sketches save memory, but they increase error and variance. Approximation quality depends heavily on sketch size.
Developers also sometimes compare approximate and exact results on tiny datasets and conclude the method is useless because the estimate is not perfect. Approximate sketches are most valuable when exact computation is expensive enough that small error is acceptable.
Finally, remember that approximation errors can compound if you derive one estimate from another. If both the Jaccard estimate and the set-size estimates are noisy, the final intersection estimate inherits that uncertainty.
Summary
- Fast approximate intersection size is often computed indirectly rather than by exact set overlap.
- MinHash is a practical way to estimate Jaccard similarity.
- If you know or estimate the set sizes, you can derive an approximate intersection cardinality.
- HyperLogLog is useful for approximate set size, but not as a direct intersection estimator.
- Approximate methods are most valuable when memory, latency, or scale makes exact computation too expensive.
Related reading
- Fast Arc Cos algorithm?
- Fast Average Square Difference Function
- Fast average without division
- Fast block placement algorithm, advice needed?
- Fast Data structure for finding strict subsets from a given list
- Fast element lookup for a functional languageHaskell
- Fast branchless max for unsigned integers
- Fast calculation of floating 1/N if factorization of very large integer N is known

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.