Data Structures · Visualization
Bloom Filter: Contains?
Bloom Filter
Checking if an item might be present runs it through the same hash functions used during adding, then checks whether all the resulting bit positions are set to 1. Time: O(k), constant regardless of how many items are stored. Space: O(1) extra.
Big-O: Space-efficient, no false negatives; possible false positives.
Bloom Filter: Contains?
Bloom FilterChecking if an item might be present runs it through the same hash functions used during adding, then checks whether all the resulting bit positions are set to 1. If even one of those bits is 0, the item was definitely never added. If all are 1, the item was probably added — but this could also be a coincidence caused by other items' bits overlapping (a false positive).
How Contains? Works
- Take the item to check.
- Run it through each of the k hash functions to get k positions.
- Check the bit at every one of those positions.
- If any bit is 0, return 'definitely not present'. If all bits are 1, return 'probably present'.
Contains? Worked Example
Input: Check 'dog' where bits 2, 5, 8 were set when adding it
- hash1('dog') = 2 → bit is 1
- hash2('dog') = 5 → bit is 1
- hash3('dog') = 8 → bit is 1
- All bits are 1
Output: 'probably present' (correct in this case)
Press ▶ Run to watch it step by step.
def contains(x):
for i in _hashes(x):
if bits[i] == 0:
return False # definitely absent
return True # probably present
# Any 0 bit => definite NO.
# example: containsBehind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Contains? Time & Space Complexity
When to Use Contains?
- Skipping expensive database or disk lookups when the filter says 'definitely not present'
- Quickly filtering out spam or already-seen items before deeper checks
- Pre-checking cache membership before a full cache lookup
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.