Programming in Swift: Fundamentals

Oct 19 2021 · Swift 5.5, iOS 15, Xcode 13

Part 4: More Collections

32. Challenge: Sets

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 31. Working with Sets Next episode: 33. Conclusion

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 32. Challenge: Sets

Update Notes: The student materials have been reviewed and are updated as of October 2021.

Transcript: 32. Challenge: Sets

It’s time for your next challenge! You can find the challenge in the “06 - Challenge - Sets” page of the playground you’ve been using, or you can download a new one from the resources for this video. Open it up, and try solving the challenge questions on your own, then keep watching to compare your work to mine. Good luck!

So first off, I just want to draw your attention to the section I’ve marked as “Challenge 0” at the top. Here, I’ve set up two sets, one initialized with a set of mythical pets

…and the second initialized as an empty set:

You see that to declare an empty set, you simply declare the constant or variable, give it a name, and set it equal to a new set, give it a datatype, in this case, string, and then two parentheses. This just instantiates a new, empty set for you.

And down below, I’ve used the .insert method on the empty set to add various animal pets to the set. Now - on to the real challenges!

Use the .union method to show the combined set of pets, then print it out. I can do this all in one line; first, I’ll do the union:

mythicalPets.union(animalPets)

For the union method, it doesn’t actually matter which set goes where, since the result will be the same. It’s a lot like multiplying two numbers; 2 times 3 is the same as 3 times two. They both equal six, regardless of which number comes first.

Then, I simply wrap that whole thing in a print statement: print(mythicalPets.union(animalPets)) There - that’s the complete unique set of pets. Onward!

Use the .intersection method to find out which pets exist in both sets, and then print it out.

.intersection is very much like .union; again, it doesn’t matter which one comes first. I’ll switch around the order from last time, and operate on animalPets this time:

animalPets.intersection(mythicalPets)

And then I’ll wrap that all in a print statement:

print(animalPets.intersection(mythicalPets))

And on the second line of your console, you see that the only two animals in common between both sets are Toothless the dragon, and Ron’s pet owl. On to challenge 3!

Mango is the only pet that exists in real life, so we’ll remove her with the .remove method. She’s pretty wonderful, but let’s keep these two sets limited to the realm of make-believe.

So, I’ll start with removing Mango. I’ll put the set name, then .remove, and pass in the value I want to remove, in this case, Mango:

animalPets.remove("🐶 Mango")

Now, because I want to save the value returned from this in a constant, I’ll simply put let removedPet before it and assign the value, like this:

let removedPet = animalPets.remove("🐶 Mango")

So as far as this set is concerned, Mango is gone — but not forgotten, since she’s still in that removedPet constant!

Now that we have just mythical creatures in there, we’ll use formUnion to mutate, or change, the original mythicalPets set, instead of creating a new set to hold the union. Now, in this case, order does matter, because the set I’m operating on is the one that will be updated in place.

So I’ll put mythicalPets first, then call formUnion, and then tell Swift I want to union animalPets into this set:

mythicalPets.formUnion(animalPets)

…and if I print out the mythicalPets array now:

print(mythicalPets)

I can see that all six of the mythical pets are now part of the mythicalPets set.

That’s it! Head on into the next video, where I’ll wrap up what you’ve learned in this section of the course and get you ready for the next and final section. I’ll see you there!