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 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. 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

  1. Take the item to check.
  2. Run it through each of the k hash functions to get k positions.
  3. Check the bit at every one of those positions.
  4. 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

  1. hash1('dog') = 2 → bit is 1
  2. hash2('dog') = 5 → bit is 1
  3. hash3('dog') = 8 → bit is 1
  4. All bits are 1

Output: 'probably present' (correct in this case)

Try your own input
Bloom Filter: Introduction
A compact, probabilistic set: it can tell you an item is "definitely not present" or "probably present", using far less memory than storing every item.
0
0
0
1
0
2
0
3
0
4
0
5
0
6
0
7
0
8
0
9
0
10
0
11
0
12
0
13
0
14
0
15
Active hash bit Bit set to 1 Bit = 0
Bloom Filter: Introduction
Press ▶ Run to watch it step by step.
Python · Bloom Filters in Code
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: contains
Input: Check 'dog' where bits 2, 5, 8 were set when adding it

Behind 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

TimeO(k), constant regardless of how many items are stored
SpaceO(1) extra

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

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.