# Add element to array if element is not in there already

**URL:** https://forum.kirupa.com/t/add-element-to-array-if-element-is-not-in-there-already/310876
**Category:** flash
**Created:** [June 22, 2010, 4:43am UTC](https://forum.kirupa.com/t/add-element-to-array-if-element-is-not-in-there-already/310876 "2010-06-22T04:43:16Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Skribble](https://avatars.discourse-cdn.com/v4/letter/s/b487fb/32.png) [@Skribble](https://forum.kirupa.com/u/Skribble)
#### Post date: [June 22, 2010, 4:43am UTC](https://forum.kirupa.com/t/add-element-to-array-if-element-is-not-in-there-already/310876/1 "2010-06-22T04:43:16Z")

</div>

Hey guys, another question here. This is for a small RTS prototype I’m working on for some fun, but I’ve hit a snag.

For examples sake, I’ve got two arrays, **activeAgents** and **houseArray**. A number of movieclips (player controled units/agents) are stored in **activeAgents** and **houseArray** is used to store any agent that is “inside” the house.

If an agent in **activeAgents** hitTests the house that **houseArray** belongs to and the agent _ISN’T_ already in the **houseArray** , then I want to add the agent from **activeAgents** INTO the **houseArray**.

```auto
for(i=0; i<activeAgents.length; i++){
if(house.hitTest(activeAgents*._x, activeAgents*._y, true)){
	for(h=0; this.houseArray[h] != activeAgents*; h++){
		this.houseArray[h] = activeAgents*;
	}
}
}

```

This was my most recent attempt, but obviously results in an infinite loop that sends Flash in to a spiral of death.

I’m honestly not sure how to achieve what I want to happen, so any and all ideas or info are welcomed and appreciated.

Thanks!
