Back to Course

Introduction to Swift

0% Complete
0/0 Steps
1. Swift: Basics
4 Topics
|
5 Quizzes
2. Swift: Variables
7 Topics
|
8 Quizzes
3. Swift: Conditional Statements
10 Topics
|
11 Quizzes
4. Swift: Loops
9 Topics
|
10 Quizzes
5. Swift: Logical Operators
6 Topics
|
7 Quizzes
6. Swift: Arrays
8 Topics
|
9 Quizzes
7. Swift: Sets
11 Topics
|
12 Quizzes
8. Swift: Dictionaries
11 Topics
|
12 Quizzes
9. Swift: Functions
13 Topics
|
14 Quizzes
10. Swift: Structures
11 Topics
|
12 Quizzes
11. Swift: Classes
7 Topics
|
8 Quizzes

Participants 73

Lesson 7, Topic 10
In Progress

Swift: symmetricDifference Method

Yasin Cakal 28 Dec 2021
Lesson Progress
0% Complete

The `.symmetricDifference()` operation can be used to find elements that are present in one of the given sets but not the other.

The following is the syntax for creating a set with `.symmetricDifference()`:

``var SetSymmetricDifference = SetA.symmetricDifference(SetB)``

The sets `Spotify` and `YouTubeMusic` store String values of songs that can be played on that console:

``````var Spotify: Set = ["Rhiannon", "Eleanor Rigby", "Sweet Caroline"];

var YouTubeMusic: Set = ["Me and Bobby McGee", "Rhiannon", "Sweet Caroline"];``````

We can use `.symmeticDifference()` to remove any values that appear in both `Spotify` and `YouTubeMusic` to find which songs are exclusive to only one console:

``var SongsExclusive = Spotify.symmetricDifference(YouTubeMusic);``

If we `print()` the values contained in `SongsExclusive`, we get something like this:

``["Eleanor Rigby", "Me and Bobby McGee"]``
Quizzes