How to Delete an Element from Array in C++

In this tutorial, we will learn how to delete an element from an array in C++. We can use the index value to delete any element from an array.

So we all know how to add an element in an Array.
But it is very hard to delete an element from an array compared to adding it
So let us have a look at the ways to delete an element from an Array

Delete an Element from an Array in C++

An array is a static linear data type and it is not as easy as in Linked List to delete elements from an array.

There are 2 basic ways of deleting an element

  1. Shifting Elements
  2. Without Shifting Elements

Shifting an Element

So here what we are doing is, we are taking the index we want deleted from the array and shifting all the elements after it one position to the left and hence covering up that one vacant spot.

Delete a particular array element with index in C++

Take a look at the following code:

#include <iostream>
using namespace std;
int main()
{
    int arr[5] = {}; //1
    
    cout<<"Enter 5 elements\n";   //2
    for(int i=0;i<5;i++)
    {
        cin>>arr[i];
    }
    
    int ind, maxsize=5;  //3
    
    cout<<"Enter index to be deleted\n";  //4
    cin>>ind;
    
    if(ind<0 || ind>=maxsize)  //5
    {
        cout<<"Invalid\n";
    }
    else
    {
        for(int i=ind;i<=maxsize-1;i++)
        {
            arr[i]=arr[i+1];
        }
        arr[maxsize-1]={};
    }
    
    cout<<"Array is:\n";  //6
    for(int i=0;i<5;i++)
    {
        if(arr[i]!='\0')
            cout<<arr[i]<<" ";
    }
}

Let us go part by part.
I have described each part by using comments. If you notice some line breaks in the middle it means that its the end of a part.

Part 1:
We are creating an array of data type integer with name arr and size 5
We are declaring each variable to \0

Part 2:
Here we are accepting 5 elements from the user and storing them index wise into the array

Part 3:
We are defining 2 variables,
ind to store the index that wants to be deleted and
maxsize that stores the maximum size of the array

Part 4:
Here we are accepting the index that has to be deleted from the user and storing it in ind

Part 5:
Here we are first checking if the index is less than 0, which does not exist, or if it is greater than the maximum size of the array which also does not exist
If the index is valid, we are starting a for loop from the index that needs to be deleted and going until the max size of the array
In every iteration, we are moving the right side element one position to the left hence deleting the unwanted element
But the last value gets filled with garbage. To prevent this, we are setting the last value to \0.

Part 6:
Here we are displaying the edited array.

The output of the above code is:

Enter 5 elements   
1                   
2                   
3                   
4                   
5                    
Enter index to be deleted  
2                          
Array is:                  
1 2 4 5

Also read:

Covering an Element:

This is relatively easier and direct approach compared to shifting elements
Here we are straight away converting the element to \0 and printing making the code smaller.

Take a look at the following code:

#include <iostream>
using namespace std;
int main()
{
    int arr[5] = {}; //1
    
    cout<<"Enter 5 elements\n";   //2
    for(int i=0;i<5;i++)
    {
        cin>>arr[i];
    }
    
    int ind, maxsize=5;  //3
    
    cout<<"Enter index to be deleted\n";  //4
    cin>>ind;
    
    if(ind<0 || ind>=maxsize)  //5
    {
        cout<<"Invalid\n";
    }
    else
    {
        arr[ind]={};
    }
    
    cout<<"Array is:\n";  //6
    for(int i=0;i<5;i++)
    {
        if(arr[i]!='\0')
            cout<<arr[i]<<" ";
    }
}

As you would have noticed, part 1,2,3,4,6 is the same.

However, part 5 has a few lines different in the else block.

In the previous category, we were shifting all the elements to the left making a continuous stream of valid numbers and then all the null elements.
Whereas here we are straightway nulling the unwanted element so the null is between valid numbers.

This isn’t exactly preferred since we have a NULL space between elements and hence keeping track of it becomes hard causing memory wastage
It is better to segregate it like in the first example.

Hence the output comes:

Enter 5 elements      
1                    
2                     
3                    
4                    
5                    
Enter index to be deleted  
3                        
Array is:              
1 2 3 5

Important Point to remember:

  1. While printing we are using the line ” if(arr[i]!=’\0′) ” is very necessary.
    Without that line, whatever is \0 automatically gets printed as 0.
    This can cause confusion if one of your elements is 0.
  2. While declaring the array, we are using the line ” int arr[5]={} ” is also very important
    Using this line we are automatically making the elements in the array \0 hence making our problem easier to solve.

 

And that is all. I hope you understood the logic and were able to execute it by yourself. If you have any doubts regarding this, feel free to ask it in the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *