RealDocs

TArray::AddUnique

function Core Since unknown
#include "Containers/Array.h"
Access: public

Description

Adds an element only if it is not already present in the array. Returns the index of the existing or newly added element.

Signature

UE_FORCEINLINE_HINT SizeType AddUnique(ElementType&& Item)

Parameters

Name Type Description Default
Item ElementType&& The element to add if not already present.

Return Type

int32

Caveats & Gotchas

  • Performs a linear scan (O(n)) to check for duplicates — do not use in hot paths on large arrays. Consider TSet for O(1) uniqueness guarantees.
  • Uses operator== for comparison, so the element type must implement equality. For pointer arrays, this compares pointer values (addresses), not pointed-to objects.
  • Returns the index of the existing element if a duplicate is found, not -1 or an error. Check the array size before and after to determine whether insertion occurred.

Example

Maintain a unique list of interacting actors C++
TArray<AActor*> UniqueOverlaps;

void AMyActor::OnOverlapBegin(AActor* OtherActor)
{
	UniqueOverlaps.AddUnique(OtherActor);
}

void AMyActor::OnOverlapEnd(AActor* OtherActor)
{
	UniqueOverlaps.Remove(OtherActor);
}

Version History

Introduced in: unknown

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.