<!--
{
  "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/RangeSet/Ranges-swift.struct/lastIndex(of:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SKsSQ7ElementRpzrlE9lastIndex2of0C0QzSgAB_tF::SYNTHESIZED::s:s8RangeSetV6RangesV"
  },
  "title" : "lastIndex(of:)"
}
-->

# lastIndex(of:)

Returns the last index where the specified value appears in the
collection.

```
func lastIndex(of element: Self.Element) -> Self.Index?
```

## Parameters

`element`

An element to search for in the collection.

## Return Value

The last index where `element` is found. If `element` is not
found in the collection, this method returns `nil`.

## Discussion

After using `lastIndex(of:)` to find the position of the last instance of
a particular element in a collection, you can use it to access the
element by subscripting. This example shows how you can modify one of
the names in an array of students.

```
var students = ["Ben", "Ivy", "Jordell", "Ben", "Maxime"]
if let i = students.lastIndex(of: "Ben") {
    students[i] = "Benjamin"
}
print(students)
// Prints "["Ben", "Ivy", "Jordell", "Benjamin", "Max"]"
```

> Complexity: O(*n*), where *n* is the length of the 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)