The union
method finds the
union(set1, set2)
This method takes two sets as an argument.
This method returns a set
containing all the elements of the given sets.
The code below demonstrates the use of the union
method:
# union of two set println(union(Set([1, 2]), Set([1, 4]))) # union of two set println(union(Set([1, 2]), Set([3, 4]))) # union of two set println(union(Set([1, 2]), Set([1, 2])))
Line 2: We use the union
method to get a set
containing all the sets’ elements— [1,2]
and [1,4]
. The union
method returns [4,2,1]
.
Line 5: We use the union
method to get a set
containing all the sets’ elements— [1,2]
and [3,4]
. The union
method returns [4,2,3,1]
.
Line 8: We use the union
method to get a set
containing all the sets’ elements— [1,2]
and [2,1]
. The union
method returns [2,1]
.
RELATED TAGS
CONTRIBUTOR
View all Courses