‫Instructor: Hi, within this lecture,
‫we're going to learn about sets.
‫So sets are very similar to arrays over here,
‫but they will be a little bit different.
‫They will have their advantages and disadvantages.
‫So I'm going to take notes again,
‫I'm going to label arrays over there
‫and right now I'm going to go for sets, okay.
‫So we're going to learn about sets
‫and we're going to continue learning about arrays
‫in this lecture as well.
‫So what is a set?
‫So this is kind of an array again,
‫but they have unique values inside of them.
‫So you cannot have two Jameses inside of a set,
‫you cannot have two ones inside of a set.
‫So let me show you how this works out.
‫So if you do something like this, var mySet
‫and open parenthesis again
‫and do one, two, three, four, five,
‫and again, Swift will think that this is an array
‫rather than a set, right?
‫Because as you can see, if I do my set,
‫it will think that this is an integer array.
‫But I don't want this to be an array,
‫I want it to be a set, so I can predefine this, right?
‫I can just go for here and say this is a set.
‫So right now, if I do my set, as you can see,
‫this is a set having integers inside of it.
‫So what's the difference between sets and arrays?
‫So first, as I said before,
‫you cannot have same value more than one
‫inside of a set, and sets are unordered collections.
‫So let me show you what I mean.
‫Can I come over here to my number array
‫and can I append something
‫or can I change something like this?
‫If I do my number array, zero,
‫I will get one, obviously, right?
‫Because this is the first element,
‫and I cannot do that in set,
‫I cannot go over here and just bring me the first element,
‫because sets do not have indexes.
‫They have something called last or first,
‫they used to have last, but I believe they have
‫got rid of them.
‫So yeah, they have the first, for example,
‫if I run this, I won't even get one, I will get three.
‫And every time I run this,
‫this will change because the sets are unordered collections.
‫They do not have indexes like we do in arrays, okay?
‫So you cannot have, you cannot have sorted sets.
‫You can have sorted arrays,
‫you can create some structural values,
‫you can have some sorted values in arrays.
‫You can go over here
‫and you can try to append something like,
‫you can just say append and you can add a new element
‫like eight over here,
‫and it will be ordered like one, two, three,
‫four, five, six, seven, eight.
‫So if I call this
‫and hit play button on the right hand side,
‫I will see that this will be like
‫one, two, three, four, five, six, seven, eight.
‫So yeah, I can just make this a little bit bigger.
‫And as you can see, eight appended at the end of this array,
‫it got the last index.
‫So if I say my append dot last,
‫it will give me a reasonable result over here, like eight.
‫Okay?
‫And as you can see, we got the eight.
‫So let me click over this
‫and just close this down and this one too.
‫And in here, I cannot do that.
‫And mySet.first doesn't even make sense.
‫So right now, you may think that
‫why are we even using the sets?
‫Why do we have sets?
‫They're not even ordered.
‫So let me do it in this way.
‫So one, two, three, four, five, one, two.
‫If I run my set, I won't get one and two twice,
‫I only get them once,
‫but I can add as many as I want in arrays.
‫So as you call this, you will see that we will see
‫one, two, three, four, five, six, seven,
‫and one, two, three, one more time.
‫As you can see, now this is my array,
‫I can see the duplicate values as well.
‫But in sets, I cannot do that.
‫And this is a beneficial sometimes,
‫if I don't want any duplicates inside of an collection,
‫then I can use sets.
‫So sets are unordered collections,
‫okay, unordered collections,
‫and they have unique values.
‫So if you're looking for unordered collections
‫with unique elements, then you have to use sets.
‫For example, I think that you have a string set
‫in which you will have A, B, and then C, and then A again.
‫So this is the same principle, right?
‫If you run this, you won't see A twice,
‫you will only get it once, but it will be unordered.
‫So for example, let's suppose that you have an array,
‫but inside of this array, you have duplicated values.
‫How do you get rid of those values?
‫Of course, you can try to convert your array
‫into a set, right?
‫Then the problem will be solved.
‫So let me try this.
‫So I'm gonna say this is my internet array.
‫Let's suppose that I'm downloading this data
‫from the internet and I have something like this,
‫a set of in integers, one, two, three, one, two, three.
‫And I have one more than three times,
‫maybe two more than two times.
‫So if I try to convert this into a set,
‫I can wrap this around set
‫like I did with strings and integers.
‫I can just convert this array into a set, right?
‫Now, if I run this, I will see that
‫in my array, I have all the values.
‫So let me print this so we can see it better, okay.
‫So let me print my internet array
‫and let me print my internet set.
‫So let me run those values.
‫And as you can see, I have everything in my internet array,
‫but I don't have the duplicated values inside my set.
‫So if you come across a situation
‫in which you have to get rid of the duplicated values
‫and then you can just do whatever you want to do
‫with those left, then you can convert this structure
‫into a set.
‫So for example, let me have something called mySet1.
‫And in this set, I have one, two, three,
‫and again I have mySet2, and in this set,
‫I have three, four, five.
‫Now I want to combine these two sets together
‫to have one, two, three, four, five.
‫Rather than having two, three, two times, how do I do this?
‫I can create mySet3 and I can just say my set,
‫my set one or two dot union.
‫And as you can see, it asks for an other sequence.
‫And in this case, the other sequence is mySet2.
‫So it means that just union make a union out of this
‫mySet1 and mySet2.
‫And if you do print mySet3, you will get what?
‫You will get, one, two, three, four, five, of course,
‫in an unordered fashion.
‫So we don't have indexes in sets,
‫but we have unique elements.
‫So whenever you need this,
‫you are more than welcome to use sets.
‫So we are gonna stop here,
‫and within the next lecture, we're going to see
‫another array or set-like structure called dictionaries.