Skip to Content
Learn
Sets
Set Operations: .symmetricDifference()

The .symmetricDifference() operation can be used to find elements that exist in one of the given sets, but not both.

The syntax for creating a set using .symmetricDifference() looks like this:

var NewSet = SetA.symmetricDifference(SetB)

.symmetricDifference()

The following sets nintendoSwitch and playStation4 store String values of games that can be played on that specific console:

var nintendoSwitch: Set = ["Animal Crossing", "DOOM Eternal", "Stardew Valley"] var playStation4: Set = ["DOOM Eternal", "Stardew Valley", "The Last of Us"]

To find which games are exclusive to only one console, we can use .symmeticDifference() to remove any values that appear in both switchConsole and playStation4:

var exclusiveGames = nintendoSwitch.symmetricDifference(playStation4)

If we print() the values contained within exclusiveGames, we would get something similar to the following output:

["Animal Crossing", "The Last of Us"]

Instructions

1.

Take a look at the two sets oscarWinners and emmyWinners in Awards.swift.

Use .symmetricDifference() to create a set called difference that contains the values that appear in either oscarWinners or emmyWinners but do not appear in both sets.

Print the value of difference.

Folder Icon

Take this course for free

Already have an account?