Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the first element in an array? [duplicate]

Tags:

arrays

c#

linq

I have an array:

arr[0]="a"   arr[1]="b"   arr[2]="a"   

I want to remove only arr[0], and keep arr[1] and arr[2].
I was using:

arr= arr.Where(w => w != arr[0]).ToArray();   

Since arr[0] and arr[2] have the same value ("a"), the result I'm getting is only arr[1].

How can I return both arr[1] and arr[2], and only remove arr[0]?

like image 492
user990635 Avatar asked Jan 15 '15 14:01

user990635


People also ask

How do I remove the first element from an array?

shift() The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.

How do I remove a duplicate element from an array?

We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.

How do you remove the first and last element of an array?

To remove the first and last elements from an array, call the shift() and pop() methods on the Array. The shift method removes the first and the pop method removes the last element from an array.


1 Answers

You can easily do that using Skip:

arr = arr.Skip(1).ToArray();   

This creates another array with new elements like in other answers. It's because you can't remove from or add elements to an array. Arrays have a fixed size.

like image 167
Selman Genç Avatar answered Sep 25 '22 00:09

Selman Genç