Required Instance Methodswift 6.0.3Swift

intersection(_:)

Returns a new set with the elements that are common to both this set and the given set.

func intersection(_ other: Self) -> Self

Parameters

other

A set of the same type as the current set.

Returns

A new set.

In the following example, the bothNeighborsAndEmployees 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"]"