Vishwas
Vishwas

Reputation: 1541

Removing array of items from an array

I have came up with this simple snippet.

var a:Array = new Array( 1,2,3,4,5,6,7,8);
var b:Array = new Array( 6,7,8 ) //<<<These items need to be removed from a 

public function removeItemsFromAThatAreListedInB(a:Array, b:Array )
{
    for ( var i=0 ; i< a.length ;i++)
    {

        for ( var j=0 ; j< b.length ; j++)
        {
            if ( (a[i]) == (b[j])  )
            {
                a.splice(i,1) 
            }

        }

    }


}

Just wanna make sure, if someone has a better "optimized" and "speedier" way to do the same ?

Upvotes: 2

Views: 185

Answers (7)

user1888370
user1888370

Reputation:

To remove the current:

removeChild(a[i]); a.pop();

or with b

removeChild(b[j]); b.pop();

pop(); is the method for removing the last array item, then you need to remove the item itself, using removeChild, also remove every event you gave it (a.removeEventListener(Event.ENTER_FRAME);, etc

Pop is often used for bullets, where when the bullet hits the enemy, you do two loops

for(var i:int < bulletsarray.length; i++) {
for(var j:int < var enemynumber:int = 4; j++) {
if(bulletsarray[i].hitTestObject(enemyarray[j])) {
removeChild(bulletsarray[i]);
bulletsarray.pop();
removeChild(enemy_array[i]);
}
}
}
}

This code is raw, might need some edits.

Upvotes: 0

francis
francis

Reputation: 6349

Most of these answers look to be for JavaScript. Since the pasted code looks to be AS3 I think you may want something like this.

Try using Array.indexOf instead of a second loop

public function removeItemsFromAThatAreListedInB(a:Array, b:Array){
    for(var i:int = 0 ; i < b.length ; b++){ 
         var index:int = a.indexOf(b[i]); //returns -1 if the element is not found.
                                         //this method uses the strict equality to compare
         if(index != -1){         
            a.splice(index, 1); //remove the element from a
        }
    }

} 

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#indexOf%28%29

Upvotes: 1

mplungjan
mplungjan

Reputation: 177786

Does this work in action script?

for (var pos,i=0, n=b.length;i<n;i++) {
  pos = a.indexOf(b[i]);
  if (pos!=-1) a.splice(pos,1);
}

Upvotes: 1

crowjonah
crowjonah

Reputation: 2878

As with the accepted answer to this question, you could simplify your function to use filter and indexOf.

a = a.filter(function(item) {
    return b.indexOf(item) === -1;
});

Doing so would only sacrifice support for IE8 and below.

Here's a pure javascript example: http://jsfiddle.net/crowjonah/dfqSE/2/

Upvotes: 2

Guffa
Guffa

Reputation: 700232

For an O(n) solution instead of O(n*m), put the values from B in an object:

var i, o = {};
for (i = 0; i < b.length; i++) {
  o[b[i]] = 1;
}
for (i = a.length - 1; i >= 0; i--) {
  if (o.hasOwnProperty(a[i])) {
    a.splice(i,1) 
  }
}

If you know that the arrays are sorted, then you can loop through them in paralell:

var i = 0, j = 0;
var r = [];
while (i < a.length) {
  if (a[i] < b[j]) {
    r.push(a[i]);
    i++;
  } else {
    if (a[i] == b[j]) i++;
    j++;
  }
}
a = r;

Upvotes: 2

asgoth
asgoth

Reputation: 35829

Using Underscore:

_.difference([1,2,3,4,5,6,7,8], [6,7,8]);
=> [1,2,3,4,5]

Upvotes: 1

user1972382
user1972382

Reputation:

Your implementation needs O(n*m) time with n = A.size, m = B.size. If you sort array A first (O(n*logn)) and use binary search on array A (O(m*logn)), you will need O(n*logn + m*logn), which is O(n*logn) if n>m

Upvotes: 0

Related Questions