I needed to remove the first element from an array in my program. Here is the code:
input = input.Except(new string[] {input[0]}).ToArray();
This returns the difference between the original array and an array with the to be removed element. However, it also removes duplicates (I think).
When I input
average 10 20 10 30
it returns
10 20 30
Job done, but I don't want it to remove duplicates. How do I get it to stop removing duplicates?
-
1I do realize that this question is a simalar question. Oops!Bald Bantha– Bald Bantha2015年06月26日 19:53:46 +00:00Commented Jun 26, 2015 at 19:53
2 Answers 2
I needed to remove the first element from an array in my program.
Instead of using LINQ Except, you could use Skip instead:
Bypasses a specified number of elements in a sequence and then returns the remaining elements.
input = input.Skip(1).ToArray();
Comments
Assuming the input variable has the type of List, you should be able to use:
input.Remove(input[0])
Remove will remove the first matching element it finds in the list.