Swift Sets

title: Sets


Sets are a collection type that stores an unordered list of distinct values of a given type. They are useful when the order of your elements do not matter, and also when you want to ensure that an element only occurs once.


Creating an empty Set…

var integerSet = Set<Int>()

Using Arrays to Create Sets…

let posSet: Set<Int> = [0,1,2,3,4,5] let negSet: Set = [-1,-2,-3,-4,-5]

Set Modification

Insert and Remove an element from a set.

integerSet.insert(10312018) //insert the integer "10312018" into the set integerSet.remove(10312018) //remove the integer "10312018" from the set

Set Operations

There are four fundamental set operations that can be performed on sets: union, intersection, subtraction, and symmetric difference

Note: The sorted() method used in the proceeding code, returns an array of the the elements of the set sorted in ascending order. It is primarily used in the below code for clarity.

Union: combines the elements of two sets

integerSet = integerSet.union(posSet) // performs a union operation that returns a new set integerSet.formUnion(negSet) // performs a union operation that modifies the current set integerSet.sorted() // [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]

Intersection: forms a set from only the common values of two sets

let oddSet: Set = [1,3,5,9] let _ = integerSet.intersection(oddSet).sorted(); // [1,3,5]

Symmetric Difference: forms a set from the values that are not common in both sets.

let inbetweenSet: Set<Int> = [-4,-3,-2,-1,0,1,2,3,4] let _ = integerSet.symmetricDifference(inbetweenSet).sorted() // [-5,5]

Subtraction: : forms a set with values not in the specified set.

integerSet.subtract(negSet) integerSet.sorted() // [0, 1, 2, 3, 4, 5]

Iterating Over a Set

You can iterate over a set by using a for-in loop…

var showSet = ""; for item in integerSet { showSet += "\(item) " } print(showSet)

The output for the above code is going to be along the lines of “1 5 4 3 0 2″… which of course is not in order. To iterate over a set in an orderly fashion use the sorted() method…

showSet = ""; for item in integerSet.sorted() { showSet += "\(item) " } print(showSet)

Output: “0 1 2 3 4 5”

More Information:

This article needs improvement. You can help improve this article. You can also write similar articles and help the community.