To find the union of two sets in Kotlin, you can use the union operator or the union function.
In this example,
set1
and set2
.+
to find the union of set1
and set2
and store the result in unionSet
.fun main() {
val set1 = setOf(1, 2, 3)
val set2 = setOf(3, 4, 5)
val unionSet = set1 + set2
println("Union of set1 and set2: $unionSet")
}
Union of set1 and set2: [1, 2, 3, 4, 5]
In this example,
set1
and set2
.union()
function to find the union of set1
and set2
and store the result in unionSet
.fun main() {
val set1 = setOf(1, 2, 3)
val set2 = setOf(3, 4, 5)
val unionSet = set1.union(set2)
println("Union of set1 and set2: $unionSet")
}
Union of set1 and set2: [1, 2, 3, 4, 5]
In this tutorial, we learned How to find Union of Two Sets in Kotlin language with well detailed examples.