I have a set of numbers that start at 2 and increment by 60. Given any value, I want to test if the value belongs to the set. For example, these numbers belong to the set:
2, 62, 122, 182, 242, etc.
These numbers do not belong to the set:
0, 1, 3...61, 63...121,123...181 etc
I'm looking for the shortest way that this can be tested using Kotlin.
Something as simple as this, should work.
val number = 62
val isPart = number % 60 == 2
println(isPart) //will print true
But if you really have an actual set, I'd just recommend to actually check if the number it is part of the set, because that would have a time complexity of O(1).
val ourSet = hashSetOf(2,62,122)//and so on
val number = 122
val isPart = number in ourSet
println(isPart) //will print true
//OR
val isPartSecondOption = ourSet.contains(number)
println(isPartSecondOption) //will print true
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments