.symmetricDifference()

webBlaster58934's avatar
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.

Syntax

setName.symmetricDifference(otherSetName)

The otherSetName parameter is a finite sequence of elements.

Example

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

Contribute to Docs

Learn Swift on Codecademy