Q:

Swift program to compare two sets using the equal to (==) operator

belongs to collection: Swift Set Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 FirstSetSecondSetThirdSet that contains integer elements. Then we compared sets using the equal to (==) operator and printed the appropriate message on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Swift program to check a set collection is empty o... >>
<< Swift program to check a set contains a specific s...