I think this is the perfect moment to get to know bloom-filter . This is a wonderful probabilistic data structure that can be used to immediately prove that an element is not in the set.
How it works? The idea is quite simple, acceleration is becoming more complex, and an implementation can be found in Guava .
Idea
Initialize a filter, which will be an array of bits of length, which will allow you to keep the maximum value of the hash function . When adding an item to a set, calculate its hash. Determine which bit is 1 and make sure that they all switch to 1 in the filter (array). If you want to check if an element is in a set, just calculate its hash, and then check if all bits 1 in the hash, 1 in the filter. If any of these bits is 0 in the filter, the element is definitely not in the set. If all of them are set to 1 , the element may be in the filter, so you will need to iterate over all the elements. Boost
A simple probabilistic model answers the question of how large the filter should be (and the hash range) to provide an optimal chance for false positive , which is a situation where all bits are 1 , but this element is not in the set.
Implementation
The Guava implementation provides the following bloom-filter constructor: create(Funnel funnel, int expectedInsertions, double falsePositiveProbability) . You can configure the filter yourself, depending on expectedInsertions and falsePositiveProbability .
False positive
Some people are aware of bloom-filters due to a false positive opportunity. You can use the Bloom filter so that you don’t rely on the mightBeInFilter flag. If so, you should skip all the elements and check one by one if the element is in the set or not.
Possible Use In your case, I would create a filter for the set, and then after all the tickets have been added, just skip all the numbers (since you still need to loop) and check if they are filter#mightBe int set. If you set falsePositiveProbability to 3%, you will achieve complexity around O(n^2-0.03m*n) , where m means the number of spaces. Correct me if I am mistaken in the assessment of complexity.