Swift Set Methods

Set is a collection of unique data. In Swift, there are various set methods that allow us to perform different operations on set. For example, the union() method returns all the elements present in both sets.

In this reference, you will find various set methods that are available in Swift.

Swift Set filter()

returns all elements that satisfies the condition

Swift Set first

returns the first element of array

Swift Set formUnion()

inserts elements of the given sequence into set

Swift Set insert()

inserts a new element at the end of the set

Swift Set isSubset()

returns true if one set is subset of other

Swift Set isSuperset()

returns true if a set is superset of other set

Swift Set max()

returns the maximum element in the set

Swift set remove()

removes the specified element from the set

Swift Set removeAll()

removes elements from set based on a condition

Swift Set removeFirst()

removes the first element from the set

Swift Set shuffled()

shuffles all the elements of a set

Swift Set sorted()

sorts the elements of the set in a specific order

Swift Set union()

returns a new set with distinct elements

Swift Set contains()

checks if specified element is present or not

Swift Set count

returns the total number of elements present

Swift Set forEach()

iterate through each element of a set

Swift Set formIntersection()

forms intersection between two sets

Swift Set intersection()

returns new set with elements common to both sets

Swift Set isDisjoint()

returns true if two sets are disjoint sets

Swift Set isEmpty

checks if the set is empty or not

Swift Set min()

returns the minimum element in the set

Swift Set subtract()

returns the set difference of two sets

Swift Set update()

inserts the given element in the set