It depends on whether your set is sorted.
If your set is unsorted, then finding the nearest one (above and below) is an O (n) operation and a fairly simple algorithm.
If your set is sorted, you can use a modified bisection search to find the answer in O (log n), which is obviously much better, especially on large sets.
If you do this several times, it might be worth sorting a set that carries the cost of O (n log n), which can be disabled or not, depending on how often the set changes. Some kind of tree can help improve future views as new elements are added.
source
share