# TArray RemoveAll equivalent elements?

**URL:** <https://forums.unrealengine.com/t/tarray-removeall-equivalent-elements/322345>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [June 19, 2015, 7:17am UTC](https://forums.unrealengine.com/t/tarray-removeall-equivalent-elements/322345 "2015-06-19T07:17:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_ca498f241](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ca498f241](https://forums.unrealengine.com/u/anonymous_user_ca498f241)\
**Post date:** [June 19, 2015, 7:17am UTC](https://forums.unrealengine.com/t/tarray-removeall-equivalent-elements/322345/1 "2015-06-19T07:17:56Z")

</div>

I have a TArray of structs and need to randomly pick an element i and then remove all elements for which the element == Array[i]

I wrote an overload for my struct’s == operator before realizing that both RemoveAll functions require a predicate.

How would I write a predicate to work like this, if possible?

(Also what is the best markdown for posting code)

---

<div class="post-metadata">

**Author:** ![Zaucy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zaucy](https://forums.unrealengine.com/u/Zaucy)\
**Post date:** [June 19, 2015, 8:00am UTC](https://forums.unrealengine.com/t/tarray-removeall-equivalent-elements/322345/2 "2015-06-19T08:00:48Z")

</div>

Something like this?

```
for(auto it=MyArray.CreateIterator(); it; ++it) {
    if(element == *it) {
        MyArray.RemoveAt(it.GetIndex());
    }
}

```

**edit:** You can actually just use [Remove](https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Containers/TArray/Remove/index.html).

```
MyArray.Remove(Element);

```

The name is kind of deceiving in my opinion. Makes it seem like it will remove only one item that matches and that RemoveAll is more appropriate, but indeed it will remove all items that match. It uses the `==` operator to check if they match. There’s also [RemoveSingle](https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Containers/TArray/RemoveSingle/index.html) to remove the first item it finds.

If you don’t care to preserve the order then [RemoveSwap](https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Containers/TArray/RemoveSwap/index.html) and [RemoveSingleSwap](https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Containers/TArray/RemoveSingleSwap/index.html) is a faster option.

## Predicate

The [TArray::RemoveAll](https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Containers/TArray/RemoveAll/index.html) requires a predicate which just needs to be a callable type with one of these signatures:

```
bool (ArrayElementType)
bool (ArrayElementType&)
bool (const ArrayElementType&)

```

This means you can use a function, lambda, or a functor!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_ca498f241](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ca498f241](https://forums.unrealengine.com/u/anonymous_user_ca498f241)\
**Post date:** [June 19, 2015, 8:39am UTC](https://forums.unrealengine.com/t/tarray-removeall-equivalent-elements/322345/3 "2015-06-19T08:39:02Z")

</div>

This is almost exactly what I eventually did except I tend to iterate over arrays using an index, is there an advantage to using an iterator?

I didn’t actually use either in this scenario. I did  
While(MyArray.Find(Element, OutIndex))  
{MyArray.RemoveAt(OutIndex);}  
which might be pretty inefficient now that I’ve thought about it.

---

<div class="post-metadata">

**Author:** ![Zaucy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zaucy](https://forums.unrealengine.com/u/Zaucy)\
**Post date:** [June 19, 2015, 10:27pm UTC](https://forums.unrealengine.com/t/tarray-removeall-equivalent-elements/322345/4 "2015-06-19T22:27:14Z")

</div>

Actually it looks like you could just use `Remove`. Updated answer to reflect that.
