RealDocs

TArray::Contains

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

Description

Returns true if the array contains at least one element equal to Item. Uses operator== for comparison and performs a linear O(n) scan from the beginning of the array.

Signature

bool Contains(const ComparisonType& Item) const

Parameters

Name Type Description Default
Item const ComparisonType& The value to search for using operator==.

Return Type

bool

Caveats & Gotchas

  • O(n) linear scan. For frequent membership tests on large collections, use TSet which provides O(1) average Contains.
  • For pointer arrays, compares pointer values (memory addresses), not the objects they point to. To compare by value, use ContainsByPredicate with a custom comparator.
  • Equivalent to Find(Item) != INDEX_NONE but more readable when only the bool result is needed.

Example

Gate an add by checking for duplicates C++
if (!ActiveEffects.Contains(NewEffect))
{
    ActiveEffects.Add(NewEffect);
}

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.