<!--
{
  "availability" : [
    "iOS: 27.0.0 -",
    "iPadOS: 27.0.0 -",
    "macCatalyst: 27.0.0 -",
    "macOS: 27.0.0 -",
    "tvOS: 27.0.0 -",
    "visionOS: 27.0.0 -",
    "watchOS: 27.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/UniqueArray/distance(from:to:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s11UniqueArrayVsRi_zrlE8distance4from2toS2i_SitF"
  },
  "title" : "distance(from:to:)"
}
-->

# distance(from:to:)

Returns the distance between two indices.

```
func distance(from start: UniqueArray<Element>.Index, to end: UniqueArray<Element>.Index) -> Int
```

## Parameters

`start`

A valid index of the collection.

`end`

Another valid index of the collection. If end is equal
to start, the result is zero.

## Return Value

The distance between `start` and `end`.

## Discussion> Note: To improve performance, this method does not validate that the
> given index is valid before offseting it. Index validation is
> deferred until the resulting index is used to access an element.
> This optimization may be removed in future versions; do not rely on it.

> Complexity: O(1)

---

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)