Combining @aga and @ willi-mentzel solutions to increase efficiency and dynamically set validated values:
val numbers = setOf(2, 3) arrayOf(1, 2, 3).any(numbers::contains)
In this case, the array will be iterated completely only once (at most, in the worst case).
This is more efficient than (suggested by @WilliMentzel):
numbers.any(arrayOf(1, 2, 3)::contains)
If in the worst case, the array will repeat set.count .
Note that Set.contains has O (1) complexity, but IntArray :: contains has O (N).
Of course, this optimization makes sense only if the collection or array is large enough.
alex.dorokhov
source share