Returns a new set with the elements that are common to both this set and the given sequence.
SDK
- Xcode 8.0+
Framework
- Swift Standard Library
Declaration
Parameters
otherAnother set.
Return Value
A new set.
Discussion
In the following example, the both set is made up of the elements that are in both the employees and neighbors sets. Elements that are in only one or the other are left out of the result of the intersection.
let employees: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"]
let neighbors: Set = ["Bethany", "Eric", "Forlani", "Greta"]
let bothNeighborsAndEmployees = employees.intersection(neighbors)
print(bothNeighborsAndEmployees)
// Prints "["Bethany", "Eric"]"