[ACCEPTED]-How to remove elements from an array-.net-2.0

Accepted answer
Score: 12

The removal is actually ok since you are 20 going downwards to zero, only the indexes 19 that you already passed will be modified. This 18 code actually would break for another reason: It 17 starts with results.Count, but should start at results.Count -1 since 16 array indexes start at 0.

for(int i = results.Count-1; i >= 0; i--)
{
  if(someCondition)
  {
     results.RemoveAt(i);
  }
}

Edit:

As was pointed 15 out - you actually must be dealing with 14 a List of some sort in your pseudo-code. In 13 this case they are conceptually the same 12 (since Lists use an Array internally) but 11 if you use an array you have a Length property 10 (instead of a Count property) and you can not 9 add or remove items.

Using a list the solution 8 above is certainly concise but might not 7 be easy to understand for someone that has 6 to maintain the code (i.e. especially iterating 5 through the list backwards) - an alternative 4 solution could be to first identify the 3 items to remove, then in a second pass removing 2 those items.

Just substitute MyType with the actual 1 type you are dealing with:

List<MyType> removeItems = new List<MyType>();

foreach(MyType item in results)
{
   if(someCondition)
   {
        removeItems.Add(item);
   }
}

foreach (MyType item in removeItems)
    results.Remove(item);
Score: 1

May I suggest a somewhat more functional 6 alternative to your current code:

Instead 5 of modifying the existing array one item 4 at a time, you could derive a new one from 3 it and then replace the whole array as an 2 "atomic" operation once you're done:

The easy way (no LINQ, but very similar):

Predicate<T> filter = delegate(T item) { return !someCondition; };

results = Array.FindAll(results, filter);
// with LINQ, you'd have written: results = results.Where(filter);

where 1 T is the type of the items in your results array.


A somewhat more explicit alternative:

var newResults = new List<T>();
foreach (T item in results)
{
    if (!someCondition)
    {
        newResults.Add(item);
    }
}
results = newResults.ToArray();
Score: 1

It doesn't seem like the Remove should work 8 at all. The IList implementation should 7 fail if we're dealing with a fixed-size 6 array, see here.

That being said, if you're dealing 5 with a resizable list (e.g. List<T>), why 4 call Remove instead of RemoveAt? Since 3 you're already navigating the indices in 2 reverse, you don't need to "re-find" the 1 item.

Score: 0

Usually you wouldn't remove elements as 5 such, you would create a new array from 4 the old without the unwanted elements.

If 3 you do go the route of removing elements 2 from an array/list your loop should count 1 down rather than up. (as yours does)

Score: 0

a couple of options:

List<int> indexesToRemove = new List<int>();
for(int i = results.Count; i >= 0; i--)
{
  if(someCondition)
  {
     //results.Remove(results[i]);
       indexesToRemove.Add(i);
  }
}

foreach(int i in indexesToRemove) {
results.Remove(results[i]);
}

or alternatively, you 2 could make a copy of the existing list, and 1 instead remove from the original list.

//temp is a copy of results
for(int i = temp.Count-1; i >= 0; i--)
{
  if(someCondition)
  {
     results.Remove(results[i]);
  }
}

More Related questions