To check if a set is a superset of another set in Swift, we can use the isSuperset(of:)
method. This method returns a boolean value indicating whether the set is a superset of the given set. Here's an example:
main.swift195 chars9 lines
In this example, set1
is a superset of set2
because it contains all the elements in set2
. The isSuperset(of:)
method returns true
, so the output is "set1 is a superset of set2".
We can also use the isSubset(of:)
method to check if a set is a subset of another set. This method works in the opposite way - it returns a boolean value indicating whether the set is a subset of the given set.
main.swift189 chars9 lines
In this example, set2
is a subset of set1
because all the elements in set2
are also in set1
. The isSubset(of:)
method returns true
, so the output is "set2 is a subset of set1".
gistlibby LogSnag