- 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 Subset of Another Set

This tutorial will discuss how to write swift program to check if the given set is the subset 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 subset of Y if all the items of Set X is also available in Set Y.

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

Below is a demonstration of the same −

Suppose our given input is −

Set 1 = [3, 20, 876] Set 2 = [984, 20, 84, 876, 3]

The desired output would be −

YES! Set 1 is the subset of Set 2

## Syntax

Following is the syntax −

Set1.isSubset(of: Set2)

## Algorithm

Following is the algorithm −

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

var subSet = mySet1.isSubset(of:mySet2)

**Step 3**− If subSet = 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 subset of another set.

import Foundation import Glibc var mySet1 : Set = [34, 5, 9] var mySet2 : Set = [34, 5, 78, 9, 10] print("Set 1 = ",mySet1) print("Set 2 =",mySet2) // Checking if the mySet1 is the subset of mySet2 var subSet = mySet1.isSubset(of:mySet2) if (subSet == true){ print("mySet1 is the subset of mySet2") } else{ print("mySet1 is not the subset of mySet2") }

## Output

Set 1 = [34, 5, 9] Set 2 = [5, 10, 9, 78, 34] mySet1 is the subset of mySet2

Here, in the above code, we have two sets: mySet1 and mySet2. Now using isSubset() function we check if mySet1 is the subset of mySet2. So we get output: "mySet1 is the subset of mySet2" because all the elements of mySet1 are present in mySet2.

## Example 2

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

import Foundation import Glibc var mySet1 : Set = [34, 5, 9] var mySet2 : Set = [34, 5, 78, 9, 10] var mySet3 : Set = [5, 9] // Checking if the mySet1 is the subset of mySet2 var subSet1 = mySet1.isSubset(of:mySet2) // Checking if the mySet1 is the subset of mySet3 var subSet2 = mySet1.isSubset(of:mySet3) // Checking if the mySet3 is the subset of mySet1 var subSet3 = mySet3.isSubset(of:mySet1) print("Is mySet1 is the subset of mySet2?:", subSet1) print("Is mySet1 is the subset of mySet3?:", subSet2) print("Is mySet3 is the subset of mySet1?:", subSet3)

## Output

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

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

var subSet1 = mySet1.isSubset(of:mySet2) // Return true var subSet2 = mySet1.isSubset(of:mySet3) // Return false var subSet3 = mySet3.isSubset(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?
- Java Program to check if the String contains any character in the given set of characters
- Check if string contains another string in Swift
- Check if is possible to get given sum from a given set of elements in Python
- How to check if a string is a subset of another string in R?
- 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.
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X in C++
- Python program to check if the given number is a Disarium Number
- Check if a HashSet is a subset of the specified collection in C#