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.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Hey, it's time for your next challenge. Now you can find the challenge in the zero six challenge sets page of the playground you've been using for this part of the course, or you can download a new one from the resources for this video. So open it up and try solving the challenge questions on your own first, but as always, you can keep watching to compare your solutions with mine. Good luck. (upbeat music) So, first off, I just want to draw your attention to the section I've marked as challenge zero at the top. So here, I've set up two sets, one initialized with a set of mythical pets and the second initialized as an empty set. So you can 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 data type, 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. Challenge one. Use the union method to show the combined set of pets and then print it out. Now I can do this all in one line. First, I'll do the union. (keyboard clicking) Before 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. Two times three is the same as three times two. They both equal six, regardless of which number comes first. Then, I simply wrap that whole thing in a print statement. (keyboard clicking) And there that's the complete set of pets. Onward! Challenge two. Use the intersection method, to find out which pets exist in both sets and print it out. Now, intersection is a lot like union. Again, it doesn't matter which set comes first. So I'll switch around the order from last time, just to prove this and I'll operate on animal pets this time. (keyboard clicking) And then I'll wrap that all in a print statement. 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 three. So Mango was the only pet that really exists in real life, so we'll remove her with the remove method. I mean, 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 I'll pass in the value I want to remove, in this case, Mango. Now, because I want to save the value returned from this in a constant, I'll simply put, let removed pet before it and assign the value. So far as this set is concerned, Mango was gone, but not forgotten since she's still in that removed pet constant. Challenge four. Now that we have just mythical creatures in there, we'll use form union to mutate or change the original mythical pet 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 mythical pets first, then call form union, and then tell swift, I want to union animal pets into the set. (keyboard clicking) And if I print out the mythical pets array now, I can see that all six of the mythical pets are now part of the mythical pets set. That's it! Head into the next video where I'll wrap up what you've learned in this section of the course, and I'll get you ready for the next and final section. I'll see you there!