<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 8.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/IndexSet",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation8IndexSetV"
  },
  "title" : "IndexSet"
}
-->

# IndexSet

A collection of unique integer values that represent the indexes of elements in another collection.

```
struct IndexSet
```

## Overview

The range of valid integer values is `0...Int.max-1`. Anything outside this range is an error.

## Topics

### Creating an Index Set

[`init()`](/documentation/Foundation/IndexSet/init())

Initializes an empty index set.

[`init(integer:)`](/documentation/Foundation/IndexSet/init(integer:))

Initializes an index set with a single integer.

[`init(integersIn:)`](/documentation/Foundation/IndexSet/init(integersIn:)-40cz3)

Initializes an index set with a range of integers.

### Counting Items in a Set

[`count(in:)`](/documentation/Foundation/IndexSet/count(in:)-v622)

Returns the count of integers in `self` that intersect `range`.

### Accessing Elements

[`subscript(_:)`](/documentation/Foundation/IndexSet/subscript(_:))

Accesses one element in the index set.

### Combining Index Sets

[`formIntersection(_:)`](/documentation/Foundation/IndexSet/formIntersection(_:))

Intersects the `IndexSet` with `other`.

[`formSymmetricDifference(_:)`](/documentation/Foundation/IndexSet/formSymmetricDifference(_:))

Exclusive or the `IndexSet` with `other`.

[`formUnion(_:)`](/documentation/Foundation/IndexSet/formUnion(_:))

Union the `IndexSet` with `other`.

[`intersection(_:)`](/documentation/Foundation/IndexSet/intersection(_:))

Intersects the `IndexSet` with `other`.

[`symmetricDifference(_:)`](/documentation/Foundation/IndexSet/symmetricDifference(_:))

Exclusive or the `IndexSet` with `other`.

[`union(_:)`](/documentation/Foundation/IndexSet/union(_:))

Union the `IndexSet` with `other`.

### Inserting Elements

[`insert(_:)`](/documentation/Foundation/IndexSet/insert(_:))

Insert an integer into the `IndexSet`.

[`insert(integersIn:)`](/documentation/Foundation/IndexSet/insert(integersIn:)-28eld)

Insert a range of integers into the `IndexSet`.

[`update(with:)`](/documentation/Foundation/IndexSet/update(with:))

Insert an integer into the `IndexSet`.

### Removing Elements

[`remove(_:)`](/documentation/Foundation/IndexSet/remove(_:))

Remove an integer from the `IndexSet`.

[`remove(integersIn:)`](/documentation/Foundation/IndexSet/remove(integersIn:)-7dhfw)

Remove a range of integers from the `IndexSet`.

[`remove(integersIn:)`](/documentation/Foundation/IndexSet/remove(integersIn:)-54370)

Remove a range of integers from the `IndexSet`.

[`removeAll()`](/documentation/Foundation/IndexSet/removeAll())

Remove all values from the `IndexSet`.

### Testing Set Membership

[`contains(_:)`](/documentation/Foundation/IndexSet/contains(_:))

Returns `true` if `self` contains `integer`.

[`contains(integersIn:)`](/documentation/Foundation/IndexSet/contains(integersIn:)-9frtv)

Returns `true` if `self` contains all of the integers in `indexSet`.

[`contains(integersIn:)`](/documentation/Foundation/IndexSet/contains(integersIn:)-sma8)

Returns `true` if `self` contains all of the integers in `range`.

[`intersects(integersIn:)`](/documentation/Foundation/IndexSet/intersects(integersIn:)-3sdmv)

Returns `true` if `self` intersects any of the integers in `range`.

### Manipulating Indexes

[`startIndex`](/documentation/Foundation/IndexSet/startIndex)

The beginning index in the set.

[`endIndex`](/documentation/Foundation/IndexSet/endIndex)

The ending index in the set.

[`index(after:)`](/documentation/Foundation/IndexSet/index(after:))

Returns the index that follows the given index in the set.

[`index(before:)`](/documentation/Foundation/IndexSet/index(before:))

Returns the index that precedes the given index in the set.

[`formIndex(after:)`](/documentation/Foundation/IndexSet/formIndex(after:))

Modifies the given index to refer to the item after the one it currently refers to.

[`formIndex(before:)`](/documentation/Foundation/IndexSet/formIndex(before:))

Modifies the given index to refer to the item before the one it currently refers to.

[`indexRange(in:)`](/documentation/Foundation/IndexSet/indexRange(in:)-539lz)

Return a `Range<IndexSet.Index>` which can be used to subscript the index set.

### Finding Elements

[`integerLessThanOrEqualTo(_:)`](/documentation/Foundation/IndexSet/integerLessThanOrEqualTo(_:))

Returns an integer contained in `self` which is less than or equal to `integer`, or `nil` if a result could not be found.

[`integerGreaterThan(_:)`](/documentation/Foundation/IndexSet/integerGreaterThan(_:))

Returns an integer contained in `self` which is greater than `integer`, or `nil` if a result could not be found.

[`integerGreaterThanOrEqualTo(_:)`](/documentation/Foundation/IndexSet/integerGreaterThanOrEqualTo(_:))

Returns an integer contained in `self` which is greater than or equal to `integer`, or `nil` if a result could not be found.

[`integerLessThan(_:)`](/documentation/Foundation/IndexSet/integerLessThan(_:))

Returns an integer contained in `self` which is less than `integer`, or `nil` if a result could not be found.

### Selecting Elements

[`filteredIndexSet(in:includeInteger:)`](/documentation/Foundation/IndexSet/filteredIndexSet(in:includeInteger:)-6cdvc)

Returns an IndexSet filtered according to the result of `includeInteger`.

[`filteredIndexSet(in:includeInteger:)`](/documentation/Foundation/IndexSet/filteredIndexSet(in:includeInteger:)-9dn86)

Returns an IndexSet filtered according to the result of `includeInteger`.

[`filteredIndexSet(includeInteger:)`](/documentation/Foundation/IndexSet/filteredIndexSet(includeInteger:))

Returns an IndexSet filtered according to the result of `includeInteger`.

### Excluding Elements

### Transforming Elements

### Iterating Over Elements

[`makeIterator()`](/documentation/Foundation/IndexSet/makeIterator())

Returns an iterator over the elements of this sequence.

### Sorting Elements

### Shifting Index Groups

[`shift(startingAt:by:)`](/documentation/Foundation/IndexSet/shift(startingAt:by:))

For a positive delta, shifts the indexes in [index, INT_MAX] to the right, thereby inserting an “empty space” [index, delta], for a negative delta, shifts the indexes in [index, INT_MAX] to the left, thereby deleting the indexes in the range [index - delta, delta].

### Splitting the Set

### Getting a Range-Based View

[`rangeView(of:)`](/documentation/Foundation/IndexSet/rangeView(of:)-5xqe8)

Returns a `Range`-based view of `self`.

[`rangeView`](/documentation/Foundation/IndexSet/rangeView-swift.property)

Returns a `Range`-based view of the entire contents of `self`.

[`IndexSet.RangeView`](/documentation/Foundation/IndexSet/RangeView-swift.struct)

A view of the contents of an IndexSet, organized by range.

### Using Reference Types

[`NSIndexSet`](/documentation/Foundation/NSIndexSet)

An immutable collection of unique integer values that represent indexes in another collection.

[`NSMutableIndexSet`](/documentation/Foundation/NSMutableIndexSet)

A mutable collection of unique integer values that represent indexes in another collection.



---

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)