Home »
Swift »
Swift Programs
Swift program to compare two sets using the equal to (==) operator
Here, we are going to learn how to compare two sets using the equal to (==) operator in Swift programming language?
Submitted by Nidhi, on June 28, 2021
Problem Solution:
Here, we will create three sets of integer elements using the Set collection. Then we will compare sets using the equal to (==) operator and print appropriate messages on the console screen.
Program/Source Code:
The source code to compare two sets using the equal to (==) operator is given below. The given program is compiled and executed successfully.
// Swift program to compare two sets
// using the equal to (==) operator
import Swift
var FirstSet:Set<Int> = [1,2,3,4]
var SecondSet:Set<Int> = [2,3]
var ThirdSet:Set<Int> = [2,3]
print("First set : ",FirstSet)
print("Second set: ",SecondSet)
print("Third set: ", ThirdSet)
if(FirstSet == SecondSet)
{
print("FirstSet and SecondSet are equal")
}
else
{
print("FirstSet and SecondSet are not equal")
}
if(SecondSet == ThirdSet)
{
print("SecondSet and ThirdSet are equal")
}
else
{
print("SecondSet and ThirdSet are not equal")
}
Output:
First set : [3, 2, 1, 4]
Second set: [2, 3]
Third set: [3, 2]
FirstSet and SecondSet are not equal
SecondSet and ThirdSet are equal
...Program finished with exit code 0
Press ENTER to exit console.
Explanation:
In the above program, we imported a package Swift to use the print() function using the below statement,
import Swift
Here, we created three sets FirstSet, SecondSet, ThirdSet that contains integer elements. Then we compared sets using the equal to (==) operator and printed the appropriate message on the console screen.
Swift Set Programs »