Swift Sets

In this tutorial you will learn about the Swift Sets and its application with practical example.

Swift Sets

In Swift, Set is a unordered list of distinct values of same types. Set is much similar to an array but it is unordered and won’t allows duplicate elements, each value contained in a set is unique. Swift sets are typed, thus once you declare the type of the set or swift infers it then you would only have elements of the same type. Sets are useful when we want to hold distinct values of single data type in single variable and order of items is not important.

Set values must be hashable which means it should comply with Hashable protocol and provide a hashValue property.It is important because set values are stored in unordered manner thus hashValue is used to access the set elements.

Declaring Set In Swift

There are multiple ways we can declare/initialize sets in swift.

Syntax:-

Here, set_var_name is replaced with the set name and <type> is replaced with the data type of set and value, value, … will be replaced with actual values

Example:-

here, we initialized a set named persons of type String with values “John”, “Doe”, “Smith”, “Alex”.

Output:-

swift_creating_set

Note:- If we don’t specify the type explicitly then Swift automatically infers the type.

Declaring/Initialize an empty Set

In Swift, empty set can be created in following ways –

Syntax 1:-

Here, SetName is replaced with the name of set variable and Type is replaced with the data type of set.

Syntax 2:-

Here, we have created a empty set named secondSet by passing an empty array literal [] and Type is replaced with the data type of set.

Declaring/Initialize Set with a Sequence

In Swift, it is possible to pass a sequence as a parameter which populate the elements within the set.

Example:-

Output:-

swift_creating_set_using_sequence

Swift Check Set Empty (isEmpty)

In Swift, isEmpty property can be used to check whether the given set is empty or not.

Example:-

Output:-

swift_check_empty_set_isempty

Swift Get Set Elements Count

In swift count property can be used to find the number of elements in a set.

Example:-

Output:-

swift_set_count

Swift Add Elements using insert method

In Swift insert function is used to add or insert an element into given set.

Example:-

Output:-

swift_set_insert_element

Swift Finding Elements in a Set

In swift contains property can be used to find an element in a set, it takes a single element of the same type to be find within the set and returns a boolean value to indicate given element is exists or not.

Example:-

Output:-

swift_set_find_using_contains

Swift Finding the Index of an Element in a Set

In Swift index(of:<ele>) method can be used to find the index of given elements, it takes a single element of the same type to be find within the set and returns index position if element is found in set.

Example:-

Swift Remove Elements using remove method

In Swift remove function is used to remove or delete an element from given set.

Example:-

Output:-

swift_set_remove_element

Swift Iterating Over a Set Elements

In Swift, we can loop through the set elements using for-in loop as following –

Example:-

Output:-

swift_iterating_set

In Swift, Set elements doesn’t have any defined order, we can use sorted() method to iterate over Set elements in a specified order.

Example:-

Output:-

swift_iterating_set_sorted

Swift Set Equality

In Swift, equality of two sets can be tested using == operator. It returns true if two sets contains same elements otherwise returns false.

Example:-

Output:-

swift_check_set_equality

 

Swift Set Operations

In swift we can perform some of the following basic set operations on any Set –

Union :- The union of two sets a and b is the set combining values of a and b.

intersection :- The intersection of two sets a and b is a set that contains all elements common in both sets.

subtracting :- The subtraction of two sets a and b is set containing all elements of set a and removing elements that belongs to set b.

Example:-

Output:-

swift_set_operations

Swift Set membership

isSubset(of:) :- This method is used to check if all of the values in a set are contained in specified set.
isSuperset(of:) :- This method is used to check if a set contains all the values exists in a specified set
isStrictSubset(of:) :- This method is used to check if a set is a subset of a specified set, but not equal to it.
isStrictSuperset(of:) :- This method is used to check if a set is a superset of a specified set, but not equal to it.
isDisjoint(with:) :- This method is used to check if two sets have no values in common.

Example:-

Output:-

Swift_Set_membership

In this tutorial we have learn about the Swift Sets and its application with practical example. I hope you will like this tutorial.