Swift Sets
Set is collection of unique element in similar type of data elements. order of this collection is unordered sequence. This can be define by using of following syntax.
Set<ElementType>()
or
Set<ElementType> = [element]
There is following ways to create empty sets in swift programming.
//Create empty sets
var languages : Set<String> = [] //Create a empty string set
var vowel = Set<Character>() //Create a empty character set
var coinCollection=Set<Int>() //Create a empty Integer set
print(languages)
print(vowel)
print(coinCollection)
Output
[]
[]
[]
In this example created three different empty are sets. We are inserting set element using of insert(dataElement) method that is similar to array but in this case insert position are no need. because that are collection of unordered elements.
//Create a empty string set
var language=Set<String>()
language.insert("Swift")
language.insert("Java")
language.insert("C++")
//Display set elements
print(language)
Output
["Swift", "C++", "Java"]
Note that output of this program are possible to produce different sequence of result when that are executed. But element are similar to result.
And given of this example insert three element of string sets. Suppose we are insert two similar element of same sets. Then insert() method are not put new element. Because that are already exist.
//Create a empty string set
var language=Set<String>()
language.insert("Swift")
language.insert("Java")
language.insert("C++")
//Inserting similar element
language.insert("Swift")
//Display set elements
print(language)
Output
["C++", "Swift", "Java"]
We can also inserting set element by using of array literals. see this example.
//Create a empty character set
var vowel=Set<Character>()
vowel=["a","e","i","o","u"]
print(vowel)
Output
["a", "e", "o", "u", "i"]
In this example are first creating an empty character set. And after that are inserting set element by using of array literals. This approach is useful when we are not have initial set element. But in case we are have initial set elements. Then we can be also use single statement to initialize set elements
//Create a character set and assign array literals
var vowel:Set=["a","e","i","o","u"]
print(vowel)
Output
["o", "u", "a", "e", "i"]
Here defining the variable type is set. And are providing of value us character literals. Compiler are detect data type of set by using of assigning array elements. And this type is provide to Sets. In this case are an character set.
Available set methods
Get Size : Cout are used to get number of element in sets.
var myElements:Set=[11,22,33,44,55]
//print number of elements
print("Size of myElements is : \(myElements.count)")
Output
Size of myElements : 5
Check set is empty or not: isEmpty are used to check the set size is zero or not, if size is zero that are return boolean value true.
var myElements=Set<Int>()
if myElements.isEmpty {
print("Set is Empty")
}else{
print("Set is not empty")
}
Output
Set is Empty
Remove Elements: Remove method are used to remove set elements.
var coinCollection:Set<Int>=[1,2,5,10,50,100]
print("Given Set Coin collection : \(coinCollection)")
print("Remove First element")
coinCollection.removeFirst()
print("Coin collection : \(coinCollection)")
print("Remove data element 100")
coinCollection.remove(100)
print("Coin collection : \(coinCollection)")
print("Remove all")
coinCollection.removeAll()
print("Coin collection : \(coinCollection)")
Output
Given Set Coin collection : [5, 50, 10, 100, 2, 1]
Remove First element
Coin collection : [100, 50, 10, 2, 1]
Remove data element 100
Coin collection : [50, 10, 2, 1]
Remove all
Coin collection : []
Check element is exist or not: Contain are used to check set element are exist or not.
var coinCollection:Set=[1,2,5,10,50,100]
print("coin collection : \(coinCollection)")
//check it element is exist
if coinCollection.contains(1) {
print("Element are exist")
}else{
print("Element are not exist")
}
Output
Element are exist
Sort Set elements: sorted() method are returning the ascending order sequence.
var myCollection:Set=[7,2,8,3,6,5,0,1]
print("Before sort : \(myCollection)")
print("After sort : \(myCollection.sorted())")
Output
Before sort : [1, 2, 6, 7, 5, 3, 8, 0]
After sort : [0, 1, 2, 3, 5, 6, 7, 8]
iterate set elements: We know about set which are stored elements in an unordered sequence. There are following ways to iterate Set-elements.
print("Method One")
let setItem : Set<Int> = [1,2,3,4,5,9]
//Using by for loop
for i in setItem{
print(i, terminator:" ")
}
print("\nMethod Two")
//By converting into an array
let arraySet = Array(setItem)
var index = 0
while(index<arraySet.count){
print("\(arraySet[index])", terminator:" ")
index+=1
}
Output
Method One
5 9 2 3 1 4
Method Two
5 9 2 3 1 4
Operation of sets elements
There are 4 main operation of sets. intersection, subtracting, symmetricDifference and union.
Intersection: intersection are used to compare two set elements and that are returning similar elements which are exist in both sets.
var setA:Set=[1,7,8,0,-6]
var setB:Set=[2,4,5,-6,9,11,1]
print("Set A : \(setA)")
print("Set B : \(setB)")
print("Intersection (A ∩ B) : \(setA.intersection(setB)) ")
Output
Set A : [7, -6, 0, 1, 8]
Set B : [2, 4, 9, 5, -6, 11, 1]
Intersection (A ∩ B) : [-6, 1]
Symmetric Difference: symmetricDifference are used to compare two set elements and that are returning different (non similar) elements which are exist in both sets.
//Example of symmetricDifference()
var setA:Set=[1,7,8,0,-6]
var setB:Set=[2,4,5,-6,9,11,1]
print("Set A : \(setA)")
print("Set B : \(setB)")
print("ISymmetric Difference (A △ B) : \(setA.symmetricDifference(setB)) ")
Output
Set A : [7, -6, 0, 1, 8]
Set B : [2, 4, 9, 5, -6, 11, 1]
Symmetric Difference (A △ B) : [2, 4, 9, 5, 7, 0, 11, 8]
Union: union method are select all elements in both sets and returning new set which are contain distinct elements.
//Example of set union
var setA:Set=["a","b","d"]
var setB:Set=["s","a","d","e"]
print("Set A : \(setA)")
print("Set B : \(setB)")
print("Union (A ∪ B) : \(setA.union(setB)) ")
Output
Set A : ["d", "a", "b"]
Set B : ["d", "s", "a", "e"]
Union (A ∪ B) : ["d", "a", "b", "s", "e"]
Subtraction: subtracting() method are return new set which are get all element of first set which are not exist in second set.
//Example of set subtracting
var setA:Set=[11,1,4,7,9]
var setB:Set=[8,1,44,22,73,4]
print("Set A : \(setA)")
print("Set B : \(setB)")
print("Subtraction (A - B) : \(setA.subtracting(setB)) ")
Output
Set A : [9, 7, 4, 11, 1]
Set B : [44, 22, 73, 4, 8, 1]
Subtraction (A - B) : [7, 9, 11]
Find Superset:
var setA:Set = [1,2,3,4,5,6]
var setB:Set = [1,2,3,4]
print("Set A : \(setA)")
print("Set B : \(setB)")
print("Superset (A ⊇ B) : \(setA.isSuperset(of:setB)) ")
Output
Set A : [5, 6, 2, 3, 1, 4]
Set B : [2, 3, 1, 4]
Superset (A ⊇ B) : true
disjoint: When given two sets A and B not contain any similar elements so this type of sets disjoint result is generate true values. otherwise result will false.
var setA:Set = [1,2,3,4,5,6]
var setB:Set = [99,43]
print("Set A : \(setA)")
print("Set B : \(setB)")
print("Disjoint (A != B) : \(setA.isDisjoint(with:setB)) ")
Output
Set A : [5, 6, 2, 3, 1, 4]
Set B : [99, 43]
Disjoint (A != B) : true
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment