How to check if two sets are identical in Swift?

"a set A is a subset of a set B, or equivalently B is a superset of A, if A is "contained" inside B, that is, all elements of A are also elements of B. A and B may coincide."

There fore you could check if A is a subset of B and vise versa.

let abcSet: Set = ["Chips", "Sandwiches", "Salad"]
var foodSet = Set(["Salad", "Chips", "Sandwiches"])


abcSet.isSubsetOf(foodSet); // true
foodSet.isSubsetOf(abcSet); // true

Set conforms to Equatable, so you can just do this:

if setA == setB {
    ...
}

Tags:

Set

Swift

Swift2