**Common values from both sets:**

You can use the `intersect(_:)`

method to create a new set containing all the values common to both sets.

```
let favoriteColors: Set = ["Red", "Blue", "Green"]
let newColors: Set = ["Purple", "Orange", "Green"]
let intersect = favoriteColors.intersect(newColors) // a AND b
// intersect = {"Green"}
```

**All values from each set:**

You can use the `union(_:)`

method to create a new set containing all the unique values from each set.

```
let union = favoriteColors.union(newColors) // a OR b
// union = {"Red", "Purple", "Green", "Orange", "Blue"}
```

*Notice how the value "Green" only appears once in the new set.*

**Values that don't exist in both sets:**

You can use the `exclusiveOr(_:)`

method to create a new set containing the unique values from either but not both sets.

```
let exclusiveOr = favoriteColors.exclusiveOr(newColors) // a XOR b
// exclusiveOr = {"Red", "Purple", "Orange", "Blue"}
```

*Notice how the value "Green" doesn't appear in the new set, since it was in both sets.*

**Values that are not in a set:**

You can use the `subtract(_:)`

method to create a new set containing values that aren't in a specific set.

```
let subtract = favoriteColors.subtract(newColors) // a - (a AND b)
// subtract = {"Blue", "Red"}
```

*Notice how the value "Green" doesn't appear in the new set, since it was also in the second set.*

This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0

This website is not affiliated with Stack Overflow