.subtracting()

The .subtracting() method creates a new set containing the elements from the target set that are not in the given set.

Syntax

newSet = targetSet.subtracting(givenSet)

The elements of givenSet are removed from the elements of targetSet and what elements remain are placed into newSet.

Example

In the example below, the elements from the fruit set are removed from the elements in the produce set and the remaining elements are placed in the new veggies set:

let produce: Set = ["Lettuce", "Apples", "Carrots", "Bananas", "Broccoli", "Onions"]
let fruit: Set = ["Apples", "Bananas"]
let veggies = produce.subtracting(fruit)
print(veggies)

This will output:

["Lettuce", "Carrots", "Broccoli", "Onions"]

Note: A set is an unordered data structure, so output elements may be ordered differently.

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Swift on Codecademy