<!--
{
  "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/RandomAccessCollection",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:Sk"
  },
  "title" : "RandomAccessCollection"
}
-->

# RandomAccessCollection

A collection that supports efficient random-access index traversal.

```
protocol RandomAccessCollection<Element> : BidirectionalCollection where Self.Indices : RandomAccessCollection, Self.SubSequence : RandomAccessCollection
```

## Overview

Random-access collections can move indices any distance and
measure the distance between indices in O(1) time. Therefore, the
fundamental difference between random-access and bidirectional collections
is that operations that depend on index movement or distance measurement
offer significantly improved efficiency. For example, a random-access
collection’s `count` property is calculated in O(1) instead of requiring
iteration of an entire collection.

# Conforming to the RandomAccessCollection Protocol

The `RandomAccessCollection` protocol adds further constraints on the
associated `Indices` and `SubSequence` types, but otherwise imposes no
additional requirements over the `BidirectionalCollection` protocol.
However, in order to meet the complexity guarantees of a random-access
collection, either the index for your custom type must conform to the
`Strideable` protocol or you must implement the `index(_:offsetBy:)` and
`distance(from:to:)` methods with O(1) efficiency.

## Relationships

### Conforming Types

[`UnsafeMutableRawBufferPointer`](/documentation/Swift/UnsafeMutableRawBufferPointer)

[`ContiguousArray`](/documentation/Swift/ContiguousArray)

[`Words-swift.struct`](/documentation/Swift/UInt128/Words-swift.struct)

[`EnumeratedSequence`](/documentation/Swift/EnumeratedSequence)

[`CollectionOfOne`](/documentation/Swift/CollectionOfOne)

[`UTF8View`](/documentation/Swift/Unicode/Scalar/UTF8View)

[`ReversedCollection`](/documentation/Swift/ReversedCollection)

[`DefaultIndices`](/documentation/Swift/DefaultIndices)

[`Slice`](/documentation/Swift/Slice)

[`UTF16View`](/documentation/Swift/Unicode/Scalar/UTF16View)

[`Words-swift.struct`](/documentation/Swift/Int16/Words-swift.struct)

[`Range`](/documentation/Swift/Range)

[`AnyRegexOutput`](/documentation/Swift/AnyRegexOutput)

[`Ranges-swift.struct`](/documentation/Swift/RangeSet/Ranges-swift.struct)

[`Words-swift.struct`](/documentation/Swift/Int/Words-swift.struct)

[`UnsafeBufferPointer`](/documentation/Swift/UnsafeBufferPointer)

[`ClosedRange`](/documentation/Swift/ClosedRange)

[`AnyRandomAccessCollection`](/documentation/Swift/AnyRandomAccessCollection)

[`Words-swift.struct`](/documentation/Swift/UInt/Words-swift.struct)

[`Repeated`](/documentation/Swift/Repeated)

[`Words-swift.struct`](/documentation/Swift/UInt32/Words-swift.struct)

[`Words-swift.struct`](/documentation/Swift/UInt64/Words-swift.struct)

[`UnsafeRawBufferPointer`](/documentation/Swift/UnsafeRawBufferPointer)

[`Words-swift.struct`](/documentation/Swift/Int8/Words-swift.struct)

[`Words-swift.struct`](/documentation/Swift/UInt16/Words-swift.struct)

[`Words-swift.struct`](/documentation/Swift/UInt8/Words-swift.struct)

[`EmptyCollection`](/documentation/Swift/EmptyCollection)

[`LazyMapSequence`](/documentation/Swift/LazyMapSequence)

[`KeyValuePairs`](/documentation/Swift/KeyValuePairs)

[`ArraySlice`](/documentation/Swift/ArraySlice)

[`Words-swift.struct`](/documentation/Swift/Int32/Words-swift.struct)

[`Array`](/documentation/Swift/Array)

[`Words-swift.struct`](/documentation/Swift/Int64/Words-swift.struct)

[`UnsafeMutableBufferPointer`](/documentation/Swift/UnsafeMutableBufferPointer)

[`LazySequence`](/documentation/Swift/LazySequence)

### Inherits From

[`BidirectionalCollection`](/documentation/Swift/BidirectionalCollection)

[`Sequence`](/documentation/Swift/Sequence)

[`Collection`](/documentation/Swift/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)