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]
?
shift() The shift() method removes the first element from an array and returns that removed element. This method changes the length of the 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With