<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/SetAlgebra",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s10SetAlgebraP"
  },
  "title" : "SetAlgebra"
}
-->

# SetAlgebra

A type that provides mathematical set operations.

```
protocol SetAlgebra<Element> : Equatable, ExpressibleByArrayLiteral
```

## Overview

You use types that conform to the `SetAlgebra` protocol when you need
efficient membership tests or mathematical set operations such as
intersection, union, and subtraction. In the standard library, you can
use the `Set` type with elements of any hashable type, or you can easily
create bit masks with `SetAlgebra` conformance using the `OptionSet`
protocol. See those types for more information.

> Note: Unlike ordinary set types, the `Element` type of an `OptionSet` is
> identical to the `OptionSet` type itself. The `SetAlgebra` protocol is
> specifically designed to accommodate both kinds of set.

# Conforming to the SetAlgebra Protocol

When implementing a custom type that conforms to the `SetAlgebra` protocol,
you must implement the required initializers and methods. For the
inherited methods to work properly, conforming types must meet the
following axioms. Assume that `S` is a custom type that conforms to the
`SetAlgebra` protocol, `x` and `y` are instances of `S`, and `e` is of
type `S.Element`—the type that the set holds.

- `S() == []`
- `x.intersection(x) == x`
- `x.intersection([]) == []`
- `x.union(x) == x`
- `x.union([]) == x`
- `x.contains(e)` implies `x.union(y).contains(e)`
- `x.union(y).contains(e)` implies `x.contains(e) || y.contains(e)`
- `x.contains(e) && y.contains(e)` if and only if
  `x.intersection(y).contains(e)`
- `x.isSubset(of: y)` implies `x.union(y) == y`
- `x.isSuperset(of: y)` implies `x.union(y) == x`
- `x.isSubset(of: y)` if and only if `y.isSuperset(of: x)`
- `x.isStrictSuperset(of: y)` if and only if
  `x.isSuperset(of: y) && x != y`
- `x.isStrictSubset(of: y)` if and only if `x.isSubset(of: y) && x != y`

## Topics

### Creating a Set

[`init()`](/documentation/Swift/SetAlgebra/init())

Creates an empty set.

### Testing for Membership

[`contains(_:)`](/documentation/Swift/SetAlgebra/contains(_:))

Returns a Boolean value that indicates whether the given element exists
in the set.

[`Element`](/documentation/Swift/SetAlgebra/Element)

A type for which the conforming type provides a containment test.

### Adding and Removing Elements

[`insert(_:)`](/documentation/Swift/SetAlgebra/insert(_:))

Inserts the given element in the set if it is not already present.

[`update(with:)`](/documentation/Swift/SetAlgebra/update(with:))

Inserts the given element into the set unconditionally.

[`remove(_:)`](/documentation/Swift/SetAlgebra/remove(_:))

Removes the given element and any elements subsumed by the given element.

### Combining Sets

[`union(_:)`](/documentation/Swift/SetAlgebra/union(_:))

Returns a new set with the elements of both this and the given set.

[`formUnion(_:)`](/documentation/Swift/SetAlgebra/formUnion(_:))

Adds the elements of the given set to the set.

[`intersection(_:)`](/documentation/Swift/SetAlgebra/intersection(_:))

Returns a new set with the elements that are common to both this set and
the given set.

[`formIntersection(_:)`](/documentation/Swift/SetAlgebra/formIntersection(_:))

Removes the elements of this set that aren’t also in the given set.

[`symmetricDifference(_:)`](/documentation/Swift/SetAlgebra/symmetricDifference(_:))

Returns a new set with the elements that are either in this set or in the
given set, but not in both.

[`formSymmetricDifference(_:)`](/documentation/Swift/SetAlgebra/formSymmetricDifference(_:))

Removes the elements of the set that are also in the given set and adds
the members of the given set that are not already in the set.

### Comparing Sets

[`isStrictSubset(of:)`](/documentation/Swift/SetAlgebra/isStrictSubset(of:))

Returns a Boolean value that indicates whether this set is a strict
subset of the given set.

[`isStrictSuperset(of:)`](/documentation/Swift/SetAlgebra/isStrictSuperset(of:))

Returns a Boolean value that indicates whether this set is a strict
superset of the given set.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)
