RealDocs

TArray::RemoveSingleSwap

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

Description

Removes the first occurrence of Item using RemoveAtSwap — the removed slot is filled by the last element, making the actual removal O(1) after the O(N) linear search. Returns 1 if removed, 0 if not found.

Caveats & Gotchas

  • Order is NOT preserved. The element previously at the last index will move into the removed slot. Code that relies on stable ordering (e.g. zipping two parallel arrays by index) will break.
  • Only removes the first match found by linear search from index 0. Use RemoveSwap to remove all matches.

Signature

SizeType RemoveSingleSwap(const ElementType& Item, EAllowShrinking AllowShrinking = UE::Core::Private::AllowShrinkingByDefault<AllocatorType>())

Parameters

Name Type Description Default
Item const ElementType& The item to search for and remove.
AllowShrinking EAllowShrinking Whether to allow the allocator to shrink after removal. UE::Core::Private::AllowShrinkingByDefault<AllocatorType>()

Return Type

SizeType

Example

Unregister a listener from an unordered list C++
TArray<IMyListener*> Listeners;
Listeners.RemoveSingleSwap(ListenerToRemove); // fast O(1) removal, order doesn't matter

Version History

Introduced in: 4.0

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.