views:

1165

answers:

4

What is the fastest way to delete one specific entry from the middle of Array()

Array is large one having Strings.

I dont want just to set Array[5] = null, but instead array size should be reduced by one and array[5] should have content of array[6] etc.

A: 

If you don't care about the order of the items in the array (but just want it to get 1 shorter) you can move the last element of the array to the index to be deleted, then pop the last element off off.

array[index] = array[array.length-1];
array.pop();

I would guess this is faster, CPU-time-wise, if you can get away with reordering the array.

EDIT: Originally, I had posted a really bad idea.

Jesse Rusak
+10  A: 

Don't have any benchmarks to support this, but one would assume that the native Array.splice method would be the fastest...

So, to remove the entry at index 5:

array.splice(5, 1);
kkyy
splice() is indeed the way to go, but keep in mind that removing things in the middle will be slow with large arrays since flash will "move up" the later entries to fill the gap.
grapefrukt
Also keep in mind that removing things in the middle of an array while running through it will wreak havoc unless you're running backwards.
Sean
+1  A: 

Array.splice() "adds elements to and removes elements from an array":

myArr.splice(indexToRemove, 1); // only removing one index, thus the 1
hasseg
A: 

Depending on your case, you may consider using a Dictionary instead of an Array if you want to prioritize the performance.

var dict:Dictionary = new Dictionary();

// The following value/key set should be customized so you can 
// get use of them in your specific case.

dict[item1] = item1;
dict[item2] = item2;

...

delete dict[item1];
Theo.T