.isSubset()

Anonymous contributor's avatar
Anonymous contributor
Published Oct 24, 2022
Contribute to Docs

The .isSubset() method returns true if all elements of a set are present in another set (passed as an argument), false otherwise.

Syntax

setName.isSubset(of: otherSetName)

The isSubset() method returns true if setName is a subset of otherSetName, false otherwise.

Example

let set1: Set = [2, 4, 6, 8, 10]
let set2: Set = [4, 10]
if set2.isSubset(of: set1) {
print("set2 is a subset of set1.")
} else {
print("set2 is not a subset of set1.")
}

In the example above, the first block after the if statement will run if set2 is a subset of set1. This will output:

set2 is a subset of set1.

All contributors

Contribute to Docs

Learn Swift on Codecademy