The subtracting(_:)
method of a set in Swift creates a new set with elements of a particular set that do not occur in a given set. We can use it between sets, or between a set and a sequence like an array.
set.subtracting(sequence)
sequence
: This could be another set, or a sequence such as an array.
This method returns a new set that contains elements of the set set
that are not in the sequence sequence
.
// Create some setslet evenNumbers : Set = [10, 2, 6, 8, 4]let oddNumbers : Set = [11, 5, 7, 3, 9]let first_names : Set = ["john", "jane", "henry", "mark"]// Create some array sequenceslet primeNumbers = [2, 3, 5, 7, 11]let last_names = ["doe", "muller", "mark", "henry"]// Perform the subtractingprint(evenNumbers.subtracting(primeNumbers))print(oddNumbers.subtracting(primeNumbers))print(first_names.subtracting(last_names))
subtracting(_:)
method on the sets and pass along the arrays as arguments. Then, we print the results to the console.