We can check if two sets are equal in Swift by using the equality operator ==
. Two sets are equal if both contain the same elements. If they are equal, the operator returns true.
Otherwise, it returns false
.
set1 == set2
set1
: This is a set on the left-hand side of the operator. It is one of the two sets we want to compare.
set2
: This is the set on the right-hand side of the operator. It is the other of the two sets that we want to compare.
The value returned is a boolean value. If the sets are equal, then a true
is returned. Otherwise, a false
is returned.
// create some setslet evenNumbers : Set = [10, 2, 6, 8, 4]let oddNumbers : Set = [11, 5, 7, 3, 9]let evenNumbers2 : Set = [2, 4, 6, 8, 10]let first_names : Set = ["john", "jane", "henry", "mark"]let last_names : Set = ["jane", "john", "henry", "mark"]// check if equalprint(evenNumbers == oddNumbers) // falseprint(evenNumbers == evenNumbers2) // trueprint(first_names == last_names) // true