- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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 the given Set is the Disjoint of Another Set

This tutorial will discuss how to write swift program to check if the given set is the disjoint of another set.

Set is a primary collection type in Swift. It is an unordered collection which stores unique values of same data type. You are not allowed to store different type of values in the same set. A set can be mutable or immutable.

Suppose we have two sets: Set X and Set Y. Now a Set X is said to the disjoint of Set Y if both the sets does not contain any common element.

To check if the given set is the disjoint of another set Swift provide an in-built function named isDisjoint(). This function will return true if the given set is the disjoint of another set. Otherwise it will return false.

Below is a demonstration of the same −

Suppose our given input is −

Set 1 = [10, 67, 92, 98, 23] Set 2 = [938, 100, 38, 12]

The desired output would be −

YES! Set 1 is the disjoint of Set 2

## Syntax

Following is the syntax −

Set1.isDisjoint(with: Set2)

## Algorithm

Following is the algorithm −

**Step 1**− Declare and initialise two sets with values.**Step 2**− Check if the mySet1 is the superset of mySet2 using isSuperset() function and store the result into a variable −

var subSet = mySet1.isSuperset(of:mySet2)

**Step 3**− If supSet = true the print "mySet1 is the subset of mySet2".**Step 4**− Otherwise print "mySet1 is not the subset of mySet2".

## Example 1

The following program shows how to check if the given set is the disjoint of another set.

import Foundation import Glibc var mySet1 : Set = [34, 5, 78, 9, 10] var mySet2 : Set = [83, 1, 39, 23, 21] // Checking if the mySet1 is the disjoint of mySet2 var subSet = mySet1.isDisjoint(with:mySet2) if (subSet == true){ print("mySet1 is the disjoint of mySet2") } else{ print("mySet1 is not the disjoint of mySet2") }

## Output

mySet1 is the superset of mySet2

Here, in the above code, we have two sets: mySet1 and mySet2. Now using isDisjoint() function we check if mySet1 is the disjoint of mySet2. So we get output: "mySet1 is the disjoint of mySet2" because both the set does not have common elements.

## Example 2

The following program shows how to check if the given set is the disjoint of another set.

import Foundation import Glibc var mySet1 : Set = ["pig", "dog", "snake", "owl"] var mySet2 : Set = ["cat", "bat", "otter"] var mySet3 : Set = ["owl", "dog"] // Checking if the mySet1 is the disjoint of mySet2 var disSet1 = mySet1.isDisjoint(with:mySet2) // Checking if the mySet1 is the disjoint of mySet3 var disSet2 = mySet1.isDisjoint(with:mySet3) // Checking if the mySet3 is the disjoint of mySet2 var disSet3 = mySet3.isDisjoint(with:mySet2) print("Is mySet1 is the disjoint of mySet2?:", disSet1) print("Is mySet1 is the disjoint of mySet3?:", disSet2) print("Is mySet3 is the disjoint of mySet2?:", disSet3)

## Output

Is mySet1 is the superset of mySet2?: true Is mySet1 is the superset of mySet3?: false Is mySet3 is the superset of mySet1?: true

Here, in the above code, we have three sets: mySet1, mySet2, and mySet3. Now using isDisjoint() function we check for disjoint −

var supSet1 = mySet1.isSuperset(of:mySet2) // Return true var supSet2 = mySet1.isSuperset(of:mySet3) // Return false var supSet3 = mySet3.isSuperset(of:mySet1) // Return true

- Related Questions & Answers
- Java Program to Check if a set is the subset of another set
- How to check if a set is a subset of another set in R?
- Check if two given sets are disjoint?
- Java Program to check if the String contains any character in the given set of characters
- C++ Program to Implement Disjoint Set Data Structure
- Check if string contains another string in Swift
- Find the number of Islands Using Disjoint Set in C++
- Check if is possible to get given sum from a given set of elements in Python
- Python Program to check if the given array is Monotonic
- Python program to check if the given string is pangram
- Python program to check if both halves of the string have same set of characters.
- Python program to check if the given string is vowel Palindrome
- Python program to check if the given number is Happy Number
- Program to check given graph is a set of trees or not in Python
- Golang program to check if k’th bit is set for a given number or not.
- Python program to check if the given number is a Disarium Number