To get the length of a set in Kotlin, you can use the size
property of the set.
In this example,
set1
with some elements.size
property of the set object set1
to get the number of elements in the set.size
property returns the length or size of the set.fun main() {
val set1 = setOf("apple", "banana", "cherry")
val length = set1.size
println("Length of set1 is: $length")
}
Length of set1 is: 3
In this example,
emptySet
.size
property of the set object emptySet
to get the number of elements in the set.size
property returns 0.fun main() {
val emptySet = emptySet<String>()
val length = emptySet.size
println("Length of emptySet is: $length")
}
Length of emptySet is: 0
In this tutorial, we learned How to Get Length of a Set in Kotlin language with well detailed examples.