RealDocs

TArray::Remove

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

Description

Removes all elements equal to Item and returns the number of elements removed. Preserves array order (shifts subsequent elements down).

Signature

SizeType Remove(const ElementType& Item)

Parameters

Name Type Description Default
Item const ElementType& The value to remove. All matching elements are removed.

Return Type

int32

Caveats & Gotchas

  • Removes ALL occurrences, not just the first. Use RemoveSingle() to remove only the first match.
  • Order-preserving removal (shifts elements) is O(n). If order does not matter, RemoveSwap() is faster as it fills the gap with the last element.
  • Do not call Remove() inside a ranged-for loop over the same array — modifying the array while iterating causes undefined behaviour. Iterate backwards with a standard index loop instead.

Example

Remove a dead actor from a tracked list C++
// Remove all references to an actor when it dies
void AMyManager::OnActorDied(AActor* DeadActor)
{
	TrackedActors.Remove(DeadActor);
	// Returns count removed; safe even if not present (returns 0)
}

// To remove only the first occurrence:
TrackedActors.RemoveSingle(DeadActor);

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.