Remove element from a vector in C# and don’t leave the vector with a blank space?

Asked

Viewed 1,915 times

5

I need to remove an element from a vector with 5 elements, and cannot get empty space in the vector.

I need to remove by index.

3 answers

9


It doesn’t work efficiently. You can do this:

var array = { 1, 2, 3, 4, 5 }
array = array.Where(item => item != 3).ToArray(); //retira o 4o. elemento

If you don’t want to use LINQ:

var array = { 1, 2, 3, 4, 5 }
array = = Array.FindAll(numbers, x => x != 3).ToArray(); //retira o 4o. elemento

Or you can make an extension method to have a removeAt()(that exists in a List) no Array:

public static T[] RemoveAt<T>(this T[] source, int index) {
    T[] dest = new T[source.Length - 1];
    if(index > 0) Array.Copy(source, 0, dest, 0, index);
    if(index < source.Length - 1) Array.Copy(source, index + 1, dest, index, source.Length - index - 1);
    return dest;
}

I put in the Github for future reference.

Source.

Not even a list could do this efficiently. A linked list would even be efficient to some extent.

2

To complement the existing answer, it might be worth reviewing the need to use an array. When dealing with adding and deleting elements, it is interesting to use a collection type. From the documentation, see how to choose the ideal type for what you’re dealing with.

There’s no way resize an array without a high price. You must create a whole new array with the copy of the items. This is what makes the Array.Resize() in the after all, see:

T[] newArray = new T[newSize];
Array.Copy(larray, 0, newArray, 0,  larray.Length > newSize? newSize : larray.Length);
array = newArray;

In cases where you have a fixed item size and need performance, using an array can be advantageous, especially if you need to access items multiple times. See the test of benchmarking done at Soen:

List/foreach: 3054ms (589725196)
Array/foreach: 1860ms (589725196)

To save specific data as a bitmap of an image, an array is what you look for. Note that these are very specific cases. Most of the time will use a List<T> rather than an array for these purposes.

It is important to note that behind the scenes the List<T> uses an array:

public class List<T> : IList<T>, System.Collections.IList, IReadOnlyList<T>
{
    private T[] _items;
    // ...
}

1

Use this for below. It will remove the index value, and each vector item will move one position backwards, leaving the last item null.

     int[] vetor = new int[5];

     private void remove(int indice)
     {
          for(int i = indice; i < vetor.length-1; i++)
          {
                vetor[i] = vetor[i+1];
          }
          vetor[vetor.length-1] = null;
     }

In the end, you will have a vector with 5 spaces and only the last one will be null. To remove the null space, you can use some variable to represent the amount of spaces in the vector and decrease one with the 'qtd--;', or simply instantiate a new vector, copying the values through another 'for'.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.