Instance Method

subtract(_:)

Removes the elements of the given set from this set.

Declaration

mutating func subtract(_ other: Set<Element>)

Parameters

other

Another set.

Discussion

In the following example, the elements of the employees set that are also members of the neighbors set are removed. In particular, the names "Bethany" and "Eric" are removed from employees.

var employees: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"]
let neighbors: Set = ["Bethany", "Eric", "Forlani", "Greta"]
employees.subtract(neighbors)
print(employees)
// Prints "["Diana", "Chris", "Alicia"]"

Relationships

From Protocol

See Also

Combining Sets

func union<S>(S) -> Set<Element>

Returns a new set with the elements of both this set and the given sequence.

func formUnion<S>(S)

Inserts the elements of the given sequence into the set.

func intersection(Set<Element>) -> Set<Element>

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

func intersection<S>(S) -> Set<Element>

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

func formIntersection<S>(S)

Removes the elements of the set that aren’t also in the given sequence.

func symmetricDifference<S>(S) -> Set<Element>

Returns a new set with the elements that are either in this set or in the given sequence, but not in both.

func formSymmetricDifference(Set<Element>)

Removes the elements of the set that are also in the given sequence and adds the members of the sequence that are not already in the set.

func formSymmetricDifference<S>(S)

Replace this set with the elements contained in this set or the given set, but not both.

func subtract<S>(S)

Removes the elements of the given sequence from the set.

func subtracting(Set<Element>) -> Set<Element>

Returns a new set containing the elements of this set that do not occur in the given set.

func subtracting<S>(S) -> Set<Element>

Returns a new set containing the elements of this set that do not occur in the given sequence.