---
title: SetAlgebra
framework: swift
role: symbol
role_heading: Protocol
path: swift/setalgebra
---

# SetAlgebra

A type that provides mathematical set operations.

## Declaration

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

## Overview

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()](swift/setalgebra/init().md)

### Testing for Membership

- [contains(_:)](swift/setalgebra/contains(_:).md)
- [Element](swift/setalgebra/element.md)

### Adding and Removing Elements

- [insert(_:)](swift/setalgebra/insert(_:).md)
- [update(with:)](swift/setalgebra/update(with:).md)
- [remove(_:)](swift/setalgebra/remove(_:).md)

### Combining Sets

- [union(_:)](swift/setalgebra/union(_:).md)
- [formUnion(_:)](swift/setalgebra/formunion(_:).md)
- [intersection(_:)](swift/setalgebra/intersection(_:).md)
- [formIntersection(_:)](swift/setalgebra/formintersection(_:).md)
- [symmetricDifference(_:)](swift/setalgebra/symmetricdifference(_:).md)
- [formSymmetricDifference(_:)](swift/setalgebra/formsymmetricdifference(_:).md)

### Comparing Sets

- [isStrictSubset(of:)](swift/setalgebra/isstrictsubset(of:).md)
- [isStrictSuperset(of:)](swift/setalgebra/isstrictsuperset(of:).md)

### Initializers

- [init(_:)](swift/setalgebra/init(_:).md)

### Instance Properties

- [isEmpty](swift/setalgebra/isempty.md)

### Instance Methods

- [isDisjoint(with:)](swift/setalgebra/isdisjoint(with:).md)
- [isSubset(of:)](swift/setalgebra/issubset(of:).md)
- [isSuperset(of:)](swift/setalgebra/issuperset(of:).md)
- [subtract(_:)](swift/setalgebra/subtract(_:).md)
- [subtracting(_:)](swift/setalgebra/subtracting(_:).md)

## Relationships

### Inherits From

- [Equatable](swift/equatable.md)
- [ExpressibleByArrayLiteral](swift/expressiblebyarrayliteral.md)

### Inherited By

- [OptionSet](swift/optionset.md)

### Conforming Types

- [ObservationTracking.Options](observation/observationtracking/options.md)
- [Set](swift/set.md)
