2

How can we delete all array elements between 2 numbers? For example : The array is {2,6,3,6,8,2,7,2} The user writes in two numbers, let's say 2 and 4. That causes the program to delete every array elements between the 2nd and 4th position. In this case, it deletes : 3,6,8

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • Is it an array or a list? With a list, you can just foreach. Or if you can create a new list / array, you can just use linq with skip and take. You can't resize an array. – Millie Smith Nov 15 '15 at 00:22
  • It's an array, but can't we do anything about Array.Remove? – Dr.Roflcopter Nov 15 '15 at 00:28
  • No. From the docs: "The key thing to be aware of when you cast an array to one of these interfaces is that members which add, insert, or remove elements throw NotSupportedException." – Millie Smith Nov 15 '15 at 00:35
  • There are plenty of good solutions shown in http://stackoverflow.com/questions/457453/remove-element-of-a-regular-array - should be easy to adapt to remove the range instead of single element. – Alexei Levenkov Nov 15 '15 at 01:46

3 Answers3

3

You cannot delete items from an array. But you can create a new array that contains only the items that you want to keep. In your case you can use the following:

int[] array = {2, 6, 3, 6, 8, 2, 7, 2};
array = array.Where((_, i) => i < 2 || i > 4).ToArray();

By the way, if you use a List instead of an array, then you can remove items. Consider the following example:

List<int> list = new List<int>() {2, 6, 3, 6, 8, 2, 7, 2};

for(int i = 4; i >= 2 ; i--)
{
    list.RemoveAt(i);
}
Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62
  • I think your code not answering what this user is looking for. He is talking about a value within the array not the index of the array. – CodeNotFound Nov 15 '15 at 00:25
  • The question is talking about the 2nd and 4th positions. I think these are indexes. – Yacoub Massad Nov 15 '15 at 00:26
  • Your first code works like a wonder. Can you explain the following line and what everything on it does? array = array.Where((_, i) => i < 2 || i > 4).ToArray(); – Dr.Roflcopter Nov 15 '15 at 01:00
  • `.Where` allows you to filter the items in the array. `(_, i) => i < 2 || i > 4` is a [lambda expression](https://msdn.microsoft.com/en-us/library/bb397687.aspx) that is used to specify which items should be allowed to pass. `i` in this case is the index of the item. You can replace `_` with any variable (like `z`) if you want to use the item value in the lambda expression. But since we don't care about the item value in our filter, we use `_` to say that we don't care about it. – Yacoub Massad Nov 15 '15 at 01:03
1

For lists, you can use RemoveRange to do exactly that. It’s just that instead of the (inclusive) end index, you need to pass the number of elements you want to delete. So for inclusive indexes start and end it would look like this:

list.RemoveRange(start, end - start + 1);

For arrays, you cannot really do this as once created arrays have a fixed size. If you really need an array, you could create a list from the array, remove the items, and then create an array again using ToArray.

poke
  • 369,085
  • 72
  • 557
  • 602
0

As Yacoub Massad mentioned in his answer: You cannot delete items from an array. But you can create a new array that contains only the items that you want to keep. In this case I would use linq, it's super easy:

int[] array = {2, 6, 3, 6, 8, 2, 7, 2};
int x = 2;
int y = 4;

var array1 = array.ToList()
  .Take(x).Concat(array.ToList().Skip(x+y-1))
  .ToArray();


foreach(var i in array1)
{
  Console.Write(i);
  Console.Write(',');
}

Result:

2,6,2,7,2,

DotNetFiddle Example

Community
  • 1
  • 1
Erik Philips
  • 53,428
  • 11
  • 128
  • 150