October 2025
Intermediate to advanced
485 pages
13h 2m
English
It’s time to turn our attention back to the problem we discussed at the beginning of this chapter. We have an array of strings and want to check whether there are any duplicates. Let’s now solve this problem in a space-efficient way by using a Bloom filter.
The strategy is to iterate over the array of strings, and if a string is deemed not a duplicate, we insert it into a Bloom filter. To determine whether a string is a duplicate, we look it up in the Bloom filter to see if we’ve ever encountered it before.
Here’s the code to implement this approach:
| | import bloom_filter |
| | |
| | |
| | def find_duplicates(array): |
| | set = bloom_filter.BloomFilter(len(array), 0.01) |
| | |
| | for item in array: |
| | if set.read(item): ... |
Read now
Unlock full access