I would like to create an extension function for a collection to check if one collection contains any item of defined set. I think about two implementations:
infix fun <T> Iterable<T>.containsAny(values: Iterable<T>): Boolean = any(values::contains)
or
infix fun <T> Iterable<T>.containsAny(values: Iterable<T>): Boolean = intersect(values).isNotEmpty()
The question is which way is more efficient and why? And is there any better solution?
The first way with
anyis O(n*m) unless the parameter Iterable is a Set, in which case it's O(n).The second way with
intersectis O(n).So the second way is much faster unless the parameter is already a Set or both inputs are so tiny that it's worth iterating repeatedly to avoid copying the receiver Iterable to a MutableSet.
The O(n) way could be improved to allow the early exit behavior of
anyby doing this:and further to avoid an unnecessary set copy:
And if the receiver Iterable is usually bigger than the parameter Iterable, you might want to swap which one is the set and which one is being iterated.