- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swift Program to Check if two sets are equal
Swift provide an equality operator(==) to check if the given two sets are equal or not. Here the equality of two the sets means that both the sets should be identical in terms of their size and elements. So if both the sets are identical or the same, then the equality operator returns true. Otherwise, the equality operator will return false.
Syntax
set1 == set2
Where set1 and set2 are two sets and using the == operator we check if they are equal or not. This operator will return true if both sets are equal. Else it will return false.
Example
In the following example, we create and initialize four sets. Then we check if they are equal to each other or not using the == operator. If they are equal we get “Yes! set1 is equal to set2” as output. Otherwise, we get “No! Both the sets are not equal” as output.
import Foundation import Glibc // Creating sets let set1: Set<Int> = [2, 5, 1, 8] let set2: Set<Int> = [8, 2, 1, 5] let set3: Set<Int> = [4, 7, 2, 8, 8] let set4: Set<Int> = [2, 5, 1, 8, 3] if set1 == set2 { print("Yes! set1 is equal to set2") } else { print("No! Both the sets are not equal") } if set3 == set4 { print("Yes! set3 is equal to set4") } else { print("No! Both the sets are not equal") } if set1 == set4 { print("Yes! set1 is equal to set4") } else { print("No! Both the sets are not equal") }
Output
Yes! set1 is equal to set2 No! Both the sets are not equal No! Both the sets are not equal
Conclusion
So this is how we can check if two sets are equal or not. Here the order of the elements despot matter only the size and the elements matter, if they are equal in both the specified sets, then the sets are equal. Otherwise not.