Issue
In Kotlin we can do:
val arr = intArrayOf(1,2,3)
if (2 in arr)
println("in list")
But if I want to check if 2 or 3 are in arr
, what is the most idiomatic way to do it other than:
if (2 in arr || 3 in arr)
println("in list")
Solution
I'd use any() extension method:
arrayOf(1, 2, 3).any { it == 2 || it == 3 }
This way, you traverse the array only once and you don't create a set instance just to check whether it's empty or not (like in one of other answers to this question).
Answered By - aga
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.