Introduction
Set is a collection that is similar to the array. A set is a unordered collection where each element is unique, but in array is an ordered collection that contains duplicate items. The type stored in an array conforms to hashable protocol and the type must provide a way to compute hash value. The swift types like string, Double, Int and Bool conform to hashable protocol and is used in the set.
Initializing a set
To initialize the set, a type of data is required to store the value and the var and let keywords are used to declare if the set is mutable or not. The set can be empty, and immutable set string can be created in it.
- var mySet = Set <String> ()
- // empty set is initialized
-
- var mySet = Set ([ "one", "two", "three"])
- // a mutable set of string type is initialized
-
- let mySet = Set([ "one", "two", "three"])
- // immutable set of string type is initialized
Inserting elements to the set
Set has insert operation to insert the elements to the set. When the element is present already in the set, the element is ignored and no error is thrown. Only unique elements will be inserted into the set and same elements are ignored.
- var setname = Set <String> ()
- setname.insert (" One ")
- setname.insert (" Two ")
- setname.insert (" Three ")
Number of elements
Swift has methods to count the number of elements in the set. The count property is used to determine the number of items in a swift set. Count method is used to count the number of items in the set. The below example has three items and it counts the items and prints the number of items in the console.
- var setname = Set <String> ()
- setname.insert (" One ")
- setname.insert (" Two ")
- setname.insert (" Three ")
- print ("\ (setname.count) items")
Set contains a number of items and checks whether the item is present in the set or not. The contains method is used to check whether the element is present in the set or not. The below example showswhether the element three is contained in the set setname or not, if contains is set to true.
- var setname = Set <String> ()
- setname.insert (" One ")
- setname.insert (" Two ")
- setname.insert (" Three ")
- var contain = setname.contains (" Three ")
Iterating and removing in set
The set can be iterated a number of times and the for statement is used to iterate over the items in a set. The below example shows the iteration of elements in the set and prints each element in the set. The specific element or all elements in the set can be removed from the set. The remove and removeAll methods are used to remove the items from the set. The specific element is removed by the remove method and the removeAll method is used to remove all the elements from the set.
- var setname = Set <String> ()
- setname.insert (" One ")
- setname.insert (" Two ")
- setname.insert (" Three ")
-
- for item insetname
- {
- print (item)
- }
- // it iterates the items in the set
-
- var item = setname.remove (" Two ")
- // removes specific element from the set
-
- setname.removeAll ()
- // remove all element in the set and it is empty
Operations in set
There are four methods in set to perform the set operations used to construct a set from two other sets. These four methods are used to create a new set or perform operations in the set.
Unique and uniqueInPlace are used to create a set with all unique values from both sets.
Subtract and subtractInPlace are used to create a set with values from first set that are not present in the second set.
Intersect and InterectInPlace are used to create a set with values that are commomly available in both sets.
ExclusiveOr and exclusiveOrInPlace are used to create a new set with values that are in either set but not present in both sets.