Set Operations In Swift

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.
  1. var mySet = Set <String> ()  
  2. // empty set is initialized  
  3.   
  4. var mySet = Set ([ "one""two""three"])  
  5. // a mutable set of string type is initialized
  6.   
  7. let mySet = Set([ "one""two""three"])  
  8. // 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.
  1. var setname = Set <String> ()    
  2. setname.insert (" One ")    
  3. setname.insert (" Two ")    
  4. 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.
  1. var setname = Set <String> ()    
  2. setname.insert (" One ")    
  3. setname.insert (" Two ")    
  4. setname.insert (" Three ")    
  5. 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.
  1. var setname = Set <String> ()      
  2. setname.insert (" One ")      
  3. setname.insert (" Two ")      
  4. setname.insert (" Three ")      
  5. 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.
  1. var setname = Set <String> ()      
  2. setname.insert (" One ")      
  3. setname.insert (" Two ")      
  4. setname.insert (" Three ")      
  5.   
  6. for item insetname  
  7. {  
  8.  print (item)  
  9. }  
  10. // it iterates the items in the set  
  11.   
  12. var item = setname.remove (" Two ")  
  13. // removes specific element from the set  
  14.   
  15. setname.removeAll ()  
  16. // 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.