To check if a set contains all the items of another set in Swift, you can use the isSuperset
method of the Set
class.
In this example,
setA
and setB
and initialize them with some integer elements.isSuperset
method of setA
to check if all elements in setB
are also present in setA
.isSuperset
method returns a boolean value: true
if setA
contains all elements of setB
, and false
otherwise.setA
is a superset of setB
.let setA: Set<Int> = [1, 2, 3, 4, 5]
let setB: Set<Int> = [2, 3]
let isSuperset = setA.isSuperset(of: setB)
print("Is setA a superset of setB? (isSuperset)")
Is setA a superset of setB? true
In this example,
setA
and setB
and initialize them with some string elements.isSuperset
method of setA
to check if all elements in setB
are also present in setA
.isSuperset
method returns a boolean value: true
if setA
contains all elements of setB
, and false
otherwise.setA
is a superset of setB
.let setA: Set<String> = ["apple", "banana", "cherry"]
let setB: Set<String> = ["banana", "cherry"]
let isSuperset = setA.isSuperset(of: setB)
print("Is setA a superset of setB? (isSuperset)")
Is setA a superset of setB? true
In this tutorial, we learned How to check if a Set contains all the items of Another Set in Swift language with well detailed examples.