Data Structures · Visualization
Bloom Filter: Introduction
Bloom Filter
A Bloom filter is a compact, probabilistic structure that answers one question extremely fast and with very little memory: 'have I possibly seen this item before?'. Time: O(k) for add/check, where k is the number of hash functions (constant, independent of item count). Space: O(m), the size of the bit array (much smaller than storing actual items).
Big-O: Space-efficient, no false negatives; possible false positives.
Bloom Filter: Introduction
Bloom FilterA Bloom filter is a compact, probabilistic structure that answers one question extremely fast and with very little memory: 'have I possibly seen this item before?'. It never gives a false negative (if it says 'no', the item is definitely not there), but it can occasionally give a false positive (saying 'maybe yes' for something never actually added). It works using a bit array and several hash functions that each flip specific bits on when an item is added.
How Bloom Filter: Introduction Works
- Create a bit array of a chosen size, with every bit initially set to 0.
- Choose several different hash functions.
- To add an item, run it through each hash function to get several positions, and set the bits at those positions to 1.
- To check an item, run it through the same hash functions; if every resulting bit position is 1, the item is 'probably present'; if any bit is 0, it is definitely absent.
Bloom Filter: Introduction Worked Example
Input: A Bloom filter with 3 hash functions and a 10-bit array
- Add 'apple': hash functions give positions 1, 4, 7 → set bits 1, 4, 7 to 1
- Add 'banana': hash functions give positions 2, 4, 9 → set bits 2, 4, 9 to 1
- Check 'apple': positions 1, 4, 7 are all 1 → probably present (correct)
- Check 'cherry': hash functions give positions 1, 2, 5 → bit 5 is 0 → definitely absent
Output: 'apple' -> probably present, 'cherry' -> definitely absent
Press ▶ Run to watch it step by step.
# A Bloom filter: a bit array + k hashes.
m, k = 1000, 3
bits = [0] * m
# add -> set k bits; contains -> check all k.
# example: introBloom Filter: Introduction Time & Space Complexity
When to Use Bloom Filter: Introduction
- Quickly checking if a username or URL has already been seen, before doing an expensive database lookup
- Web browsers checking if a URL might be malicious (Google Safe Browsing)
- Databases (e.g. Cassandra, BigTable) filtering out unnecessary disk lookups
- Network routers filtering out unwanted packets
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.