0

how do I remove specific items from my array that is hundreds of items long?

-eg:

var myArray:Array = ["dog", "cat", "bear", "duck", "frog", etc..., etc...];

What do I do when I want to remove "duck" from this array? Please keep in mind, that the array is very long and we do not know WHERE the "duck" is, so we don't know it's index in the array. I need to get that item somehow by it's name and remove it from the array.

  • @Peter I hope array contents are strings, if so quote them. The best way for now seems to be first: var i: int = myArray.indexOf(duck); ( if string make the duck 'duck') and add ilya's answer like that: if(i>=0)myArray.splice(i,1); – Volkan Mar 25 '13 at 12:20

4 Answers4

1
myArray.splice(myArray.indexOf("duck"), 1);
Ilya Zaytsev
  • 1,055
  • 1
  • 8
  • 12
  • 1
    it is a question about array and actionscript but not a question about best algoritm – Ilya Zaytsev Mar 25 '13 at 12:09
  • I do not do the work to which author did not ask, if you have an opinion you may write own answer – Ilya Zaytsev Mar 25 '13 at 12:15
  • Yes, those items are strings, I forgot to put quotes in, thanks for the heads up - just fixed it. Thank you all for your answers. I will try the indexOf thingie. –  Mar 25 '13 at 12:56
  • 1
    @PeterBeelich `indexOf()` will return a value of `-1` if the argument cannot be found. You can use complex structures a la `while ((i=myArray.indexOf("duck"))>=0) myArray.splice(i,1);` to remove all occurrences of "duck" should you need it. – Vesper Mar 25 '13 at 15:00
1

This is a straight-forward way of doing it:

Non-stable version (does less copying).

for (var i:int, j:int = array.length - 1, temp:Object; i <= j;) {
    temp = array[i];
    if (temp == "duck") {
        array[i] = array[j];
        array[j] = temp;
        j--;
    } else {
        i++;
    }
}
array.length = i;

And the stable version (more copying, but the order of the original array is unchanged):

for (var i:int, j:int, temp:Object; i < array.length; i++) {
    temp = array[i];
    if (temp != "duck") {
        array[j] = temp;
        j++;
    }
}
array.length = j;

However, if you could assure that values are unique, the algorithm would be different, since you wouldn't have to verify items after the one you've found.

The algorithm would be significantly different, if the array was sorted because you could use binary search to find the element to remove. In some very peculiar situations, like, for example, if you have a well-founded set (which is your array), the deletion would be even simpler, because the position of the item you are searching for could be determined in constant time. The later can be mitigated through the use of indices (put simple, you could have a hash-table that uses elements of the array as its keys and their offsets into array as values).

Again, you need to consider all those cases and what is practical with regard to your program.

As I've mentioned above, there may be benefits if you use a different data structure, or if you sort on insert, or if you index on insert.

Lastly, producing a copy with undesired items removed is different from destructively removing items from the same array.

0

If your array is very long, it is well worth optimizing it somehow.

some options:

1) use a dictionary; the string can be the key. This has the added benefit of not re-indexing your list when you add/remove elements.

//specify weak keys when you create the dictionary
var myDictionary:Dictionary = new Dictionary(true);
//when you want to delete an item:
myDictionary["duck"] = null;

2) sort your array and use a binary search. There's lots of info about binary search online. Here's one SO question on the topic.

Community
  • 1
  • 1
mfa
  • 5,017
  • 2
  • 23
  • 28
  • from the answer: "This only simplifies the code, and will probably be non-optimal performance." – mfa Mar 25 '13 at 13:40
  • @mfa Running the array backwards (from `length-1` to zero) is more optimal and does not care for length changing. – Vesper Mar 25 '13 at 14:58
  • removed option #3; people aren't reading that it is not optimal – mfa Mar 25 '13 at 15:39
0

One way is to just use a while loop in combination with the indexOf method of an Array as such :

var index:int;
while ((index = myArray.indexOf("duck")) >= 0)
{
    myArray.splice(index,1);
}

You could wrap that in a function and accept Array and String parameters, and return the resulting array like this :

function removeSearchString(sourceArray:Array, searchString:String):Array
{
     var index:int;
     while ((index = sourceArray.indexOf(search)) >= 0)
     {
          sourceArray.splice(index,1);
     }
     return sourceArray;
}

then you'd use the function like this :

myArray = removeSearchString(myArray, "duck");
prototypical
  • 6,731
  • 3
  • 24
  • 34