---
title: RandomAccessCollection
framework: swift
role: symbol
role_heading: Protocol
path: swift/randomaccesscollection
---

# RandomAccessCollection

A collection that supports efficient random-access index traversal.

## Declaration

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

## Overview

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.

## Topics

### Associated Types

- [Element](swift/randomaccesscollection/element.md)
- [Index](swift/randomaccesscollection/index.md)
- [Indices](swift/randomaccesscollection/indices-swift.associatedtype.md)
- [SubSequence](swift/randomaccesscollection/subsequence.md)

### Instance Properties

- [endIndex](swift/randomaccesscollection/endindex.md)
- [indices](swift/randomaccesscollection/indices-swift.property.md)
- [startIndex](swift/randomaccesscollection/startindex.md)

### Instance Methods

- [distance(from:to:)](swift/randomaccesscollection/distance(from:to:).md)
- [formIndex(after:)](swift/randomaccesscollection/formindex(after:).md)
- [formIndex(before:)](swift/randomaccesscollection/formindex(before:).md)
- [index(_:offsetBy:)](swift/randomaccesscollection/index(_:offsetby:).md)
- [index(_:offsetBy:limitedBy:)](swift/randomaccesscollection/index(_:offsetby:limitedby:).md)
- [index(after:)](swift/randomaccesscollection/index(after:).md)
- [index(before:)](swift/randomaccesscollection/index(before:).md)

### Subscripts

- [subscript(_:)](swift/randomaccesscollection/subscript(_:)-2mluu.md)
- [subscript(_:)](swift/randomaccesscollection/subscript(_:)-3fc50.md)

## Relationships

### Inherits From

- [BidirectionalCollection](swift/bidirectionalcollection.md)
- [Collection](swift/collection.md)
- [Sequence](swift/sequence.md)

### Conforming Types

- [AnyRandomAccessCollection](swift/anyrandomaccesscollection.md)
- [AnyRegexOutput](swift/anyregexoutput.md)
- [Array](swift/array.md)
- [ArraySlice](swift/arrayslice.md)
- [ClosedRange](swift/closedrange.md)
- [CollectionOfOne](swift/collectionofone.md)
- [ContiguousArray](swift/contiguousarray.md)
- [DefaultIndices](swift/defaultindices.md)
- [EmptyCollection](swift/emptycollection.md)
- [EnumeratedSequence](swift/enumeratedsequence.md)
- [Int.Words](swift/int/words-swift.struct.md)
- [Int16.Words](swift/int16/words-swift.struct.md)
- [Int32.Words](swift/int32/words-swift.struct.md)
- [Int64.Words](swift/int64/words-swift.struct.md)
- [Int8.Words](swift/int8/words-swift.struct.md)
- [KeyValuePairs](swift/keyvaluepairs.md)
- [LazyMapSequence](swift/lazymapsequence.md)
- [LazySequence](swift/lazysequence.md)
- [Range](swift/range.md)
- [RangeSet.Ranges](swift/rangeset/ranges-swift.struct.md)
- [Repeated](swift/repeated.md)
- [ReversedCollection](swift/reversedcollection.md)
- [Slice](swift/slice.md)
- [UInt.Words](swift/uint/words-swift.struct.md)
- [UInt128.Words](swift/uint128/words-swift.struct.md)
- [UInt16.Words](swift/uint16/words-swift.struct.md)
- [UInt32.Words](swift/uint32/words-swift.struct.md)
- [UInt64.Words](swift/uint64/words-swift.struct.md)
- [UInt8.Words](swift/uint8/words-swift.struct.md)
- [Unicode.Scalar.UTF16View](swift/unicode/scalar/utf16view.md)
- [Unicode.Scalar.UTF8View](swift/unicode/scalar/utf8view.md)
- [UnsafeBufferPointer](swift/unsafebufferpointer.md)
- [UnsafeMutableBufferPointer](swift/unsafemutablebufferpointer.md)
- [UnsafeMutableRawBufferPointer](swift/unsafemutablerawbufferpointer.md)
- [UnsafeRawBufferPointer](swift/unsaferawbufferpointer.md)

## See Also

### Collection Traversal

- [BidirectionalCollection](swift/bidirectionalcollection.md)
