Skip to content
StackDecode
Advanced data structuresAnimated

Bloom Filter

A bit array and several hashes that answer "definitely not" or "probably yes".

Time

O(k) per op

Extra space

O(m bits)

Use it for

Cheap membership pre-checks.

How it works

  1. A Bloom filter remembers a set in a tiny row of bits, trading a little certainty for a lot of space.
  2. To add a word, run it through k hash functions. Each gives a position; set those k bits to 1.
  3. To ask "is this word in the set?", compute the same k positions. If any bit is 0 the answer is a definite no. If all are 1 the answer is "probably yes".
  4. It never forgets a word (no false negatives), but unrelated words can happen to hit bits that are already 1, giving a false positive. A bigger m and a good k make that rarer.

Watch it run

Loading animation…