Contents

intersection(_:)

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

Declaration

func intersection<S>(_ other: S) -> Set<Element> where Element == S.Element, S : Sequence

Parameters

  • other:

    A sequence of elements. other must be finite.

Return Value

A new set.

Discussion

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 = ["Bethany", "Eric", "Forlani", "Greta"]
let bothNeighborsAndEmployees = employees.intersection(neighbors)
print(bothNeighborsAndEmployees)
// Prints "["Bethany", "Eric"]"

See Also

Combining Sets