Remove Element of a Regular Array

Remove element of a regular array

If you don't want to use List:

var foos = new List<Foo>(array);
foos.RemoveAt(index);
return foos.ToArray();

You could try this extension method that I haven't actually tested:

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;
}

And use it like:

Foo[] bar = GetFoos();
bar = bar.RemoveAt(2);

How can I remove a specific item from an array?

Find the index of the array element you want to remove using indexOf, and then remove that index with splice.

The splice() method changes the contents of an array by removing
existing elements and/or adding new elements.





const array = [2, 5, 9];

console.log(array);

const index = array.indexOf(5);
if (index > -1) { // only splice array when item is found
array.splice(index, 1); // 2nd parameter means remove one item only
}

// array = [2, 9]
console.log(array);

Remove element of Array (c#)


List<string> wordBank = new List<string>  {
"iphone", "airpods", "laptop","computer","calculator","ram","graphics", "cable","internet","world wide web"
};

//select random word
Random wordRand = new Random();
int index = wordRand.Next(wordBank.Count);
string wordSelect = wordBank[index];

wordBank.RemoveAt(index);
//or
wordBank.Remove(wordSelect);

How do you delete an element from an array without creating a new array or using ArrayLists? java

There is no direct way to remove elements from an Array in Java. Though Array in Java objects, it doesn't provide any methods to add(), remove() or search an element in Array. This is the reason Collection classes like ArrayList and HashSet are very popular.

Though

Thanks to Apache Commons Utils, You can use there ArrayUtils class
to remove an element from array more easily than by doing it yourself.
One thing to remember is that Arrays are fixed size in Java, once you
create an array you can not change their size, which means removing or
deleting an item doesn't reduce the size of the array. This is, in
fact, the main difference between Array and ArrayList in Java.

What you need to do is create a new array and copy the remaining content of this array into a new array using System.arrayCopy() or any other means. In fact, all other API and functions you will use do this but then you don't need to reinvent the wheel.


    import java.util.Arrays;
import org.apache.commons.lang.ArrayUtils;

....
//let's create an array for demonstration purpose
int[] test = new int[] { 101, 102, 103, 104, 105};

System.out.println("Original Array : size : "
+ test.length );
System.out.println("Contents : " + Arrays.toString(test));

// let's remove or delete an element from an Array
// using Apache Commons ArrayUtils
test = ArrayUtils.remove(test, 2); //removing element at index 2

// Size of an array must be 1 less than the original array
// after deleting an element
System.out.println("Size of the array after
removing an element : " + test.length);
System.out.println("Content of Array after
removing an object : " + Arrays.toString(test));

How to remove element from an array

You cannot remove an element from an array. The size of a Java array is determined when the array is allocated, and cannot be changed. The best you can do is:

  • Assign null to the array at the relevant position; e.g.

    test[1] = null;

    This leaves you with the problem of dealing with the "holes" in the array where the null values are. (In some cases this is not a problem ... but in most cases it is.)

  • Create a new array with the element removed; e.g.

    String[][] tmp = new String[test.length - 1][];
    int j = 0;
    for (int i = 0; i < test.length; i++) {
    if (i != indexOfItemToRemove) {
    tmp[j++] = test[i];
    }
    }
    test = tmp;

    The Apache Commons ArrayUtils class has some static methods that will do this more neatly (e.g. Object[] ArrayUtils.remove(Object[], int), but the fact remains that this approach creates a new array object.

A better approach would be to use a suitable Collection type. For instance, the ArrayList type has a method that allows you to remove the element at a given position.

Remove an array element and shift the remaining ones

You just need to overwrite what you're deleting with the next value in the array, propagate that change, and then keep in mind where the new end is:

int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

// delete 3 (index 2)
for (int i = 2; i < 8; ++i)
array[i] = array[i + 1]; // copy next element left

Now your array is {1, 2, 4, 5, 6, 7, 8, 9, 9}. You cannot delete the extra 9 since this is a statically-sized array, you just have to ignore it. This can be done with std::copy:

std::copy(array + 3, // copy everything starting here
array + 9, // and ending here, not including it,
array + 2) // to this destination

In C++11, use can use std::move (the algorithm overload, not the utility overload) instead.

More generally, use std::remove to remove elements matching a value:

// remove *all* 3's, return new ending (remaining elements unspecified)
auto arrayEnd = std::remove(std::begin(array), std::end(array), 3);

Even more generally, there is std::remove_if.

Note that the use of std::vector<int> may be more appropriate here, as its a "true" dynamically-allocated resizing array. (In the sense that asking for its size() reflects removed elements.)



Related Topics



Leave a reply



Submit