TArray::FindLast
#include "Containers/Array.h"
Access: public
Specifiers: const
Description
Searches the array from the end toward the front and returns the index of the last element equal to Item, or INDEX_NONE if not found. Use when duplicates may exist and you need the highest-indexed match.
Caveats & Gotchas
- • An output-parameter overload also exists — FindLast(Item, Index) — that returns bool and writes the found index to Index. Prefer the single-return-value version unless you need the bool return for conditional branching in a single expression.
- • FindLast uses linear search (O(N)); for large arrays with frequent reverse-lookups, consider a separate reverse-index map.
- • Relies on ElementType::operator== for comparisons. If the type doesn't define operator==, this won't compile. Use FindLastByPredicate with a custom lambda instead.
Signature
[[nodiscard]] SizeType FindLast(const ElementType& Item) const Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| Item | const ElementType& | The element value to search for using operator==. | — |
Return Type
SizeType Example
Find the last occurrence of a value C++
TArray<int32> Values = {1, 2, 3, 2, 4};
SizeType LastIndex = Values.FindLast(2);
// LastIndex == 3
if (LastIndex != INDEX_NONE)
{
UE_LOG(LogTemp, Log, TEXT("Last '2' at index %d"), LastIndex);
} Tags
Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?