The intersect
method is used to find the
intersect(s, itrs...)
This method takes the sets
as an argument.
This method returns a set
that contains the common elements of the provided argument sets
.
The code given below demonstrates how we can use the intersect
method:
# intersection of two set println(intersect(Set([1, 2]), Set([1, 4]))) # intersection of two set println(intersect(Set([1, 2]), Set([3, 4]))) # intersection of two set println(intersect(Set([1, 2]), Set([1, 2])))
Line 2: We use the intersect
method to find the common elements between the sets
[1,2] and [1,4]. The common elements between the two sets
is 1
, so a new set with [1] as an element is returned.
Line 5: We use the intersect
method to find the common elements between the sets
[1,2] and [3,4]. There is no common element between these two sets
, so an empty set is returned.
Line 8: We use the intersect
method to find the common elements between the sets
[1,2] and [1,2]. The common elements between the two sets
are 1
and 2
, so a new set with [1, 2] as its elements is returned.
RELATED TAGS
CONTRIBUTOR
View all Courses