Published Jan 28, 2023
Contribute to Docs

The .symmetricDifference() method returns a new set with the elements that are either in the set or in the given sequence, but not in both. In other words, it returns a new set with all the elements from two sets that do not overlap.



The otherSetName parameter is a finite sequence of elements.


The following example shows the elements that aren’t common to each set:

let food: Set = ["Apple", "Banana", "Orange", "Peach"]
let colors = ["Green", "Blue", "Orange", "Peach"]
let eitherFoodOrColor = food.symmetricDifference(colors)

This will display the following:

["Banana", "Green", "Apple", "Blue"]

All contributors

Looking to contribute?

Learn Swift on Codecademy