Swift – Check if Two Sets are Not Equal

Two Sets are said to be not equal if they contain at least one different element.

To check if two Sets are not equal in Swift, use Not Equal comparison operator and pass the Sets as operands. The operator returns true if the given Sets are not equal, else, it returns false.

Examples

In the following example, we take two sets: set1 and set2. Using Equal To operator, we shall check if these two sets are not equal.

main.swift

</>
Copy
let set1: Set = [2, 6, 4]
let set2: Set = [4, 2, 9, 8]

if set1 != set2 {
    print("The two sets are not equal.")
} else {
    print("The two sets are equal.")
}

Output

The two sets are not equal.
Program ended with exit code: 0

Now, let us take elements in the Sets, such that they are equal, and observe the output.

main.swift

</>
Copy
let set1: Set = [2, 6, 4]
let set2: Set = [4, 2, 6]

if set1 != set2 {
    print("The two sets are not equal.")
} else {
    print("The two sets are equal.")
}

Output

The two sets are equal.
Program ended with exit code: 

Conclusion

In this Swift Tutorial, we learned how to check if two Sets are not equal using Not Equal operator.