Swift Sets

What is a Swift Set and how to create?

Tested on: Xcode 12.2, Swift 5.3

Set is an unordered collection of unique value of the same data type. Unlike array which is an ordered collection. Set is useful when you want to keep the value unique and doesn't appear more than once and the order is not important. For example, the image below shows a set of ingredients for some food.

Sets of ingredents in Swift

Topics:

  1. How to create a Sets?
  2. How to access elements in Sets?
  3. Adding element or set
  4. Removing element or set





1. Creating sets in Swift.

Set does not its literals like array literals. When creating a set you use array literals and each element is separate by a comma. However, you have to explicitly declare the variable if not an array will be created.

Swift Code :

let ingredientsArray = ["Garlic", "Salt", "Tomato", "Chicken", "Pepper"]
let ingredientsSet: Set = ["Garlic", "Salt", "Tomato", "Chicken", "Pepper"]// You have to explicitly declare an set

You can also create a Set Object by using Set([ ]) and the compiler is able to infer the data type.

Swift Code :

let ingredients = Set(["Garlic", "Salt", "Tomato", "Chicken", "Pepper"])



2. Accessing elements in sets.

The term accessing means in set is not to extract the element out like what we did in array and dictionary. But is used to check whether is an element or a set of element is inside or not inside another set.

.contains()

.contains method is used to check if a set contains s pecific element. It return true, if it contains that element and false if it not.

Swift Code :

let ingredients = Set(["Garlic", "Salt", "Tomato", "Chicken", "Pepper"])
ingredients.contains("Salt")

.isSubset()

.isSubset() method is used to check if a set contains all the elements of another set. For example, Set A is a subset of set B if all the elements in Set A are found in Set B.

Sets of ingredents in Swift

Swift Code :

let ingredients = Set(["Garlic", "Salt", "Tomato", "Chicken", "Pepper"])
let ingredients2 = Set(["Salt", "Tomato"])
ingredients2.isSubset(of: ingredients)

.isSuperSubset()

.isSuperSubset() method is used to check if a portion of a set contains all the elements of another set. For example, Set B is a superset of set A if all the elements in set A are the elements of set B as shown in the image below.

Sets of ingredents in Swift

Swift Code :

let ingredients = Set(["Garlic", "Salt", "Tomato", "Chicken", "Pepper"])
let ingredients2 = Set(["Salt", "Tomato"])
ingredients.isSuperset(of: ingredients2)

.isDisjoint()

.isDisjoint() method is used to check whether two sets contains different elements as shown in the image below.

Sets of ingredents in Swift

Swift Code :

let ingredients = Set(["Garlic", "Salt", "Tomato", "Chicken", "Pepper"])
let ingredients2 = Set(["Beef", "Wine", "Water"])
ingredients.isDisjoint(with: ingredients2)



3. Adding element or set

Adding Element

You can use insert() method to add a new element to a set. In the example below, cloves is added into the ingredents set.

Swift Code :

var ingredients = Set(["Garlic", "Salt", "Tomato", "Chicken", "Pepper"])
ingredients.insert("Cloves")

print(ingredients)

Output :

["Cloves", "Tomato", "Garlic", "Salt", "Chicken", "Pepper"]


Combining Sets

Union( )

Union combine two sets together and return the combination of both of the items. For example, ingredients2 is added to ingredients and now, ingredients contains "Wine", "Garlic", "Tomato", "Water", "Pepper", "Beef", "Chicken", "Salt"

Swift Code :

let ingredients = Set(["Garlic", "Salt", "Tomato", "Chicken", "Pepper"])
let ingredients2 = Set(["Beef", "Wine", "Water"])
ingredients.union(ingredients2)

Output :

{"Wine", "Garlic", "Tomato", "Water", "Pepper", "Beef", "Chicken", "Salt"}


Intersection( )

Intersection combine the two sets but only returns the elements that are found in both set. For example, the commom elements in both ingredients and ingredients2 are "Salt" and "Tomatop". Thefore, intersection only returns these elements.

Swift Code :

let ingredients = Set(["Garlic", "Salt", "Tomato", "Chicken", "Pepper"])
let ingredients2 = Set(["Salt", "Wine", "Tomato"])
ingredients.intersection(ingredients2)

Output :

{"Salt", "Tomato"}




4. Removing element or set

Removing element

You can use remove() method to remove a element in a set. In the example below, "Salt" is removed into the ingredents set.

Swift Code :

var ingredients = Set(["Garlic", "Salt", "Tomato", "Chicken", "Pepper"])
ingredients.remove("Salt")
print(ingredients)

Output :

["Chicken", "Tomato", "Pepper", "Garlic"]


Removing set

This remove a set of elements from a set. For example, "Salt","Wine" and "Tomato" from ingredients2 are removed from ingredients. However, "Wine" is not found in ingredients therefore, it is not removed because it didn't even exist.

Swift Code :

let ingredients = Set(["Garlic", "Salt", "Tomato", "Chicken", "Pepper"])
let ingredients2 = Set(["Salt", "Wine", "Tomato"])
ingredients.subtract(ingredients2)

Output :

{"Pepper", "Garlic", "Chicken"}