Data Structures · Visualization
Bloom Filter: Add
Bloom Filter
Adding an item to a Bloom filter runs the item through each of the filter's hash functions to get several bit positions, then sets each of those positions to 1 in the bit array. Time: O(k), constant regardless of how many items are already stored. Space: O(1) additional (uses the existing bit array).
Big-O: Space-efficient, no false negatives; possible false positives.
Bloom Filter: Add
Bloom FilterAdding an item to a Bloom filter runs the item through each of the filter's hash functions to get several bit positions, then sets each of those positions to 1 in the bit array. Nothing is stored about the item itself — only the fact that these specific bits are now 'on'.
How Add Works
- Take the item to add.
- Run it through each of the k hash functions to get k positions in the bit array.
- Set the bit at each of those positions to 1 (if it's already 1, leave it as is).
Add Worked Example
Input: Add 'dog' with hash functions producing positions 2, 5, 8
- hash1('dog') = 2 → set bit 2 to 1
- hash2('dog') = 5 → set bit 5 to 1
- hash3('dog') = 8 → set bit 8 to 1
Output: Bits 2, 5, and 8 are now set to 1
Press ▶ Run to watch it step by step.
def _hashes(x):
h = abs(hash(x))
return [(h >> i) % m for i in range(k)]
def add(x):
for i in _hashes(x):
bits[i] = 1 # set each hash bit
# O(k)
# example: addBehind 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.
Add Time & Space Complexity
When to Use Add
- Registering a new item as 'seen' in a deduplication pipeline
- Marking a URL as processed in a web crawler
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.