# Thread: How to delete element of array without changing index order.

1. ## How to delete element of array without changing index order.

Hello everyone!

I need to know how to delete value of element of an array without changing indices order. For example, there is 4 elements in my array and I want to delete second element, but others will stay in the same order. How can I do that? I would appreciate your help. 2. Three options come to mind:
• Copy the subsequent elements to overwrite the deleted element, reducing the recorded size by 1. This maintains the relative order, but unfortunately deletions then run in linear time on average.
• Designate a special "deleted" value and set the deleted element to that value. Unfortunately, it is not always feasible to designate such a special value.
• Store pointers instead, then set the deleted element to be a null pointer. You still need to store the actual values somewhere though, e.g., by using malloc or a second array. 3. Code:
```int arr[] = {1, 2, 3, 4};
arr = 0; //2nd element deleted```
After that, each element stays in the same order. The only difference is the second element is 0.
If what you mean is to re-arrange the array so that there is no empty space between the elements, you can go through the array and move each element one by one, or use memmove to do it.
Code:
```const int arr_sz = 4;
for (int i = 0; i < arr_sz - 1; ++i)
{   if (arr[i] == 0)
arr[i] = arr[i + 1], arr[i + 1] = 0;
}```
Or a simpler, faster way:
Code:
`memmove(arr+1, arr+2, (arr+arr_sz) - (arr+2) );` 4. Originally Posted by Exosomes Code:
```int arr[] = {1, 2, 3, 4};
arr = 0; //2nd element deleted```
After that, each element stays in the same order. The only difference is the second element is 0.
If what you mean is to re-arrange the array so that there is no empty space between the elements, you can go through the array and move each element one by one, or use memmove to do it.
Code:
```const int arr_sz = 4;
for (int i = 0; i < arr_sz - 1; ++i)
{   if (arr[i] == 0)
arr[i] = arr[i + 1], arr[i + 1] = 0;
}```
Or a simpler, faster way:
Code:
`memmove(arr+1, arr+2, (arr+arr_sz) - (arr+2) );`
Okay, got it. But what if I want to add a new a element and assign it to the deleted one so it would look like this: (1, 5, 3, 4).
Actually, I am trying to do this with array of structure, but I do not think it will make a big difference. 5. Originally Posted by laserlight Three options come to mind:
• Copy the subsequent elements to overwrite the deleted element, reducing the recorded size by 1. This maintains the relative order, but unfortunately deletions then run in linear time on average.
• Designate a special "deleted" value and set the deleted element to that value. Unfortunately, it is not always feasible to designate such a special value.
• Store pointers instead, then set the deleted element to be a null pointer. You still need to store the actual values somewhere though, e.g., by using malloc or a second array.
Hmm, could you explain third variant a bit more, please? Popular pages Recent additions array, c++, delete, element, order 