To get the intersection of two sets in Swift, you can use the intersection
method. This method returns a new set that contains only the elements that are common to both sets.
main.swift131 chars5 lines
In the code above, setA
and setB
are two sets of Integers. We call the intersection
method on setA
passing setB
as an argument. The resulting set intersection
will contain the elements that are common to both sets, which in this case are 3, 4, and 5.
gistlibby LogSnag