A Bloom filter answers one question — "have I seen this before?" — using a tiny bit array and no stored keys at all. Adding an item flips k bits chosen by k hashes. To check membership, look at those same k bits: if any is 0, the item is definitely not in the set; if all are 1, it's probably in — probably, because other items may have set those bits. It can give a false yes, but never a false no. Add a few items, then go hunting for a false positive.
m bits, all starting at 0. The entire data structure — no keys are ever stored.
k hashes
Each item maps to k bit positions via k independent hash functions.
add
Set all k of the item’s bits to 1.
query
Any of the k bits is 0 → definitely absent. All 1 → probably present.
bloom.js — k hashes, m bits, no keys
Ready
A bit array of 24 bits, all zero. Each item is hashed by k=3 functions to three bit positions. Add item sets those bits; Query checks them.
0
items added
0/24
bits set
0%
est. FP rate
How it works
The magic is the asymmetry of the error. A "no" is always trustworthy: if even one of the item's k bits is 0, then this item was never added, because adding it would have set that bit. A "yes" is only probable: all k bits being 1 could be a coincidence — other items may have set exactly those bits. So the filter is a fast, tiny pre-check that can rule things out with certainty and rule them in only with high probability.
1
Start with all zeros
The bit array holds only bits — never the items themselves. That’s why it’s so small: it doesn’t store keys.
2
Add: light k bits
Run the item through k hashes to get k positions, and set all of them to 1. Bits may already be set by other items — that’s fine, and it’s the source of collisions.
3
Query: check k bits
Look at the item’s k positions. Any 0 means it was definitely never added. All 1 means probably added — but maybe those bits were set by others.
✓
Tune the false-positive rate
More bits (larger m) or more items (larger n) change the fill. The false-positive rate is about (1 − e^(−kn/m))^k — pick m and k for your target.
Add / query
O(k)
Space
O(m) bits
False negatives
Never
False positives
Tunable
The code
# m bits, k hash functions. No keys are ever stored.class BloomFilter:
def __init__(self, m, k):
self.m, self.k = m, k
self.bits = [0] * m
def add(self, item):
for i in range(self.k):
self.bits[hash_i(item, i) % self.m] = 1 # light k bitsdef __contains__(self, item):
for i in range(self.k):
if self.bits[hash_i(item, i) % self.m] == 0:
return False # a 0 bit => DEFINITELY absentreturn True # all 1 => PROBABLY present
Quick check
1. A Bloom filter says an item is "not present". How sure can you be?
A "no" is definitive. If any of the item’s k bits is 0, the item was never added — because adding it would have set that bit. Bloom filters have no false negatives.
2. It says an item IS present. What does that mean?
A "yes" is only probabilistic. All k bits being 1 could be a coincidence caused by other added items setting exactly those positions — a false positive. Never a false negative, sometimes a false positive.
3. Why is a Bloom filter so space-efficient?
It never stores the items — only a bit array. That’s the trade: you give up the ability to list members or delete safely, in exchange for tiny, constant-size membership checks.
FAQ
What is a Bloom filter?
A space-efficient probabilistic data structure for set membership. It uses a bit array and k hash functions: adding sets k bits, querying checks them. It can return false positives but never false negatives.
Why can a Bloom filter have false positives but not false negatives?
Adding only ever sets bits, never clears them. So a truly-added item always has all k bits at 1 — a query can’t miss it (no false negatives). But those bits may also be 1 from other items, so a never-added item can read as present (false positive).
Can you delete from a Bloom filter?
Not from a standard one — clearing a bit might break another item that shares it, causing false negatives. A counting Bloom filter uses small counters instead of bits to allow deletion, at the cost of more space.
Where are Bloom filters used?
As a fast pre-check to skip expensive work: LSM-tree databases (Cassandra, Bigtable) use them to avoid disk reads for absent keys; browsers/CDNs for malicious-URL checks; crawlers to skip seen URLs. Anywhere a cheap "definitely not here" saves an expensive lookup.
The picture: 24 bits, three hashes
The filter after six adds: 12 of 24 bits are 1 (green). One query lands entirely on set bits without ever having been added, which is the false positive; the other hits a zero and is rejected for certain. The filter holds only bits, so nothing in it says which items set them.
Worked example, step by step
Use m = 24 bits and k = 3 hashes. Hash i is FNV-1a over the characters, seeded with i + 1, finished with the murmur3 mixing steps, taken mod 24. Add cat, dog, fox, owl, ant, bee in that order, then query the other 18 animals from the page's pool, none of which was added. The table shows 1 of the animals that fool the filter, then 5 that are correctly rejected. Every position below comes from running that code.
Step
Item
Bits
Newly set
Total ones
add 1
cat
3, 3, 2
2
2
add 2
dog
9, 19, 19
2
4
add 3
fox
16, 15, 1
3
7
add 4
owl
16, 3, 22
1
8
add 5
ant
1, 7, 2
1
9
add 6
bee
12, 5, 21
3
12
query
wolf
12, 15, 2
1,1,1
all 1: "probably yes"
query
elk
8, 5, 4
0,1,0
a 0: "no"
query
ram
17, 18, 15
0,0,1
a 0: "no"
query
yak
13, 0, 4
0,0,0
a 0: "no"
query
jay
15, 20, 20
1,0,0
a 0: "no"
query
cod
1, 14, 12
1,0,1
a 0: "no"
After 6 adds, 12 of 24 bits are set (50%). 1 of the 18 absent animals came back "probably yes" (5.56%). The textbook formula (1 − e−kn/m)k with n = 6 predicts 14.69%; 18 probes cannot confirm a rate, so the complexity section measures it at scale. The structure of the answer is what matters here. Every "no" (a 0 in any position) is certain, and every "yes" is only as good as the fill: the fuller the array, the likelier three random bits are all 1.
Complexity and trade-offs
Case
Time
Space
Why
Bloom filter
O(k) add and query
O(m) bits
No false negatives. False-positive rate about (1 − e−kn/m)k. No deletion, no listing.
Hash set
O(1) expected
O(n · key size)
Exact and can delete, but stores every key in full.
Sorted array + binary search
O(log n) query
O(n · key size)
Exact, cache-friendly, expensive to insert into.
Counting Bloom filter
O(k)
O(m) counters
Allows deletion; counters cost several times the space of bits.
Cuckoo filter
O(1) expected
O(n) fingerprints
Supports deletion and often uses less space at low false-positive rates.
Measured with the hash above, 1,000 stored items and 20,000 probes for items that were never added. At m = 9,586 (about 9.6 bits per item) and k = 7: 0.88% false positives, 4933 bits set, against 1.00% from the formula. With k = 1: 10.14% (formula 9.91%). With k = 20: 7.19% (formula 7.06%), because too many hashes fill the array. Halving m to 4,793 at k = 7: 16.43% (formula 15.74%). More hashes help only until the array is half full; after that every extra hash is a net loss.
Common mistakes
Deleting by clearing bits
Clearing the three bits of fox in the 24-bit filter above leaves owl, ant unable to be found: a false negative, which a Bloom filter promises never to give. Use a counting filter, or rebuild the filter without the item.
Using one hash for all k positions
If every "hash" is the same function, the k bits collapse into one and the filter is a k = 1 filter. With the same 1,000 items and 9,586 bits that gives 10.14% false positives instead of 0.88%. Derive k functions from different seeds, or from two hashes combined as h1 + i·h2.
Inserting far more than you sized for
Sized for 1,000 items (9,586 bits, k = 7), the filter holds 0.88% false positives. Push 5,000 items into it and the measured rate becomes 83.34% with 9333 of 9,586 bits set. The array does not refuse; it just degrades. Size from the item count you will really reach.
Reading "yes" as "present"
In the 24-bit example, wolf was never added and still reads all ones. Use a Bloom filter to skip work on a "no", and confirm every "yes" against the real store.
Querying with different m or k than you built with
A filter built with m = 9,586 and queried as if m were 9,587 loses items it holds: only 1.00% of the 1,000 stored items are still found, so 99.00% of them become false negatives. The bit array is meaningless without the exact m, k and hash that filled it, so store them with the filter.
Where this shows up
Skipping disk reads
Storage engines that keep data in many sorted files can hold a small filter per file in memory. A "no" skips the file entirely; only a "yes" costs a disk read, so a false positive costs one wasted read and never a wrong answer.
Seen-before checks
Crawlers and feed systems use a filter to skip URLs or ids they have probably handled. The occasional false positive means a page is skipped, which is acceptable when the alternative is storing every address.
Blocklists and caches
A client can test a cheap local filter before asking a server whether a name is on a blocklist, or before looking up a key in a remote cache, and only contact the server on a "yes".
Set intersection and joins
One side of a distributed join sends a filter of its keys to the other side, which drops rows that cannot match before shipping them across the network.
SOLVE IT YOURSELF
Solve it: build a Bloom filter
A set that never says "no" when it means "yes" — but sometimes says "yes" when it means "no". Build one and prove the one-sided error for yourself, in Python or TypeScript, running for real in your browser.
YOUR TASK
Implement make_filter(items, m, k) — return a list of m bits with k hash positions set per item — and might_contain(bits, item, k), which returns True only if all k bits for that item are set. Use the provided bloom_hash so both sides agree.
HINTS — 5 IDEAS
For each item, set the bit at bloom_hash(item, seed, m) for every seed 1…k. Bits are only ever turned on — never off.
A lookup returns true only if every one of those k bits is set. One zero bit is a definitive "no".
That asymmetry is the whole design: a stored item always has its bits set, so false negatives are impossible.
A false positive happens when other items happen to have set all k of your bits — which is why the filter shrinks m or grows k to trade error for space.
Nothing here stores the items themselves, which is why a Bloom filter is tiny — and why you can never enumerate or delete from it.