Sets in Swift
Tags: basics, apple, beginners, swift
Published: 2021-01-02 13:31:08

# Brief Introduction About Swift

Swift is a language developed by Apple which is made using modern approach, include safety features and software design patterns. As the name suggests swift is fast, safe and easy to use. It is basically made a replacement for c-based family(C, C++ and Objective-C). It can be used to make apps and can be also used for cloud services and it is among the fastest growing languages.

# Sets in Swift

Sets are unordered collections of unique values. Dictionaries are unordered collections of key-value associations. Sets are mainly subset(part) of Dictionaries. In Sets every value defined is unique and they store value by hashing so values are not stored in a fixed order and their order can vary while accessing.

# Syntax to Define Sets

```swift
var setName : Set<DataType> = [values]
```
Here the data type passed need to be of **Hashable** type.

# Hashing

Hashing means generating a unique id for every value .

# Code Example

```swift
var SamsungSmartphone : Set<String> = ["Android", "Camera" , "Samsung Pay"] //hashing
var AppleIPhone : [String] = ["iOS" , "Camera" , "Apple Pay"]

print(SamsungSmartphone) // ["Camera", "Android", "Samsung Pay"]
print(AppleIPhone) // ["iOS", "Camera", "Apple Pay"]
```
**Note: In your case order of printing can differ as sets are unordered due to hashing process.

## Inbuilt Functions

```swift
//Below are two sets with similarities and some differences in both sets
var SamsungSmartphone : Set<String> = ["Android", "Camera" , "Samsung Pay"] //hashing
var AppleIPhone : [String] = ["iOS" , "Camera" , "Apple Pay"]

print(SamsungSmartphone)
print(AppleIPhone)

//Inbuilt Functions

// Intersection returns the element common in Both sets
print(SamsungSmartphone.intersection(AppleIPhone)) // ["Camera"]

// symmetricDifference returns all the elements that are not common in both sets
print(SamsungSmartphone.symmetricDifference(AppleIPhone)) // ["Samsung Pay", "Apple Pay", "iOS", "Android"]

// union returns all the elements in both sets
print(SamsungSmartphone.union(AppleIPhone)) // ["Android", "Samsung Pay", "Apple Pay", "Camera", "iOS"]

// Returns the elements in first set that do not match second set
print(SamsungSmartphone.subtracting(AppleIPhone)) // ["Android", "Samsung Pay"]

// Inserting Value in Set
SamsungSmartphone.insert("Samsung Watch")

// Finding Values in Set
print(SamsungSmartphone.contains("Android")) //true
print(SamsungSmartphone.contains("Andoid")) // false
```

## Custom Sets

Now Let's see how we can create custom sets.

```swift
struct Smartphone : Hashable {
    let name : String
    let model : String
}

let smartphoneObj : Set<Smartphone> = [Smartphone(name: "Apple", model: "iPhone"), Smartphone(name: "Samsung", model: "S9")]
for smartphone in smartphoneObj {
    print(smartphone.hashValue)
}


//Output:-
// Some random id
//Some random id
```

Here, we created struct and declare some properties in it and note here we are conforming it to protocol **Hashable** so that when making object from it we can utilize the functionalities of **Sets**.

Here, output will be 2 random id's as we are passing different parameters in set parameters and we know every element is unique in Sets. So, what if we give both elements same values as parameters? Will it print 2 random id's or a single random id?

Let' see

```swift
struct Smartphone : Hashable {
    let name : String
    let model : String
}

let smartphoneObj : Set<Smartphone> = [Smartphone(name: "Apple", model: "iPhone"), Smartphone(name: "Apple", model: "iPhone")]
for smartphone in smartphoneObj {
    print(smartphone.hashValue)
}

//Output :- 
//Some random id
```

It wil print one single id. Why? Because it maintains uniqueness and it will generate one hashValue corresponding to both means it will map both of them to same hash id as to maintain uniqueness.

This was all about sets in swift. Hope, this helped. Meet you guys in next post.