C++ Program to Delete an Element from an Array

In this article, you will learn how to delete an element from an array in C++ and get the code for doing so. The program is created in the following ways:

In C++, delete an element from an array.

To delete an element from an array in C++ programming, you have to ask the user to enter the array's 10 elements first. And then ask for the element that has to be deleted. Now search for that number or element and delete it if found. Otherwise, print a message such as "Element not found."

The question is, "Write a program in C++ that deletes an element from an array." Here is the answer to this question:

#include<iostream>
using namespace std;
int main()
{
    int arr[10], tot=10, i, elem, j, found=0;
    cout<<"Enter 10 Array Elements: ";
    for(i=0; i<tot; i++)
        cin>>arr[i];
    cout<<"\nEnter Element to Delete: ";
    cin>>elem;
    for(i=0; i<tot; i++)
    {
        if(arr[i]==elem)
        {
            for(j=i; j<(tot-1); j++)
                arr[j] = arr[j+1];
            found++;
            i--;
            tot--;
        }
    }
    if(found==0)
        cout<<"\nElement doesn't found in the Array!";
    else
        cout<<"\nElement Deleted Successfully!";
    cout<<endl;
    return 0;
}

This program was built and runs under the Code::Blocks IDE. Here is its sample run:

C++ program delete element from array

Now supply any 10 numbers, say 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10, as 10 array elements. Enter the number or element, say 3, that must be deleted from the list after entering 10 elements for the array arr[]. Here is the final snapshot of the sample run:

delete element from array c++

Note: If the entered element (that has to be deleted) is found in repeated order (more than once), then the element gets deleted from all the positions. That is, repeated elements also get deleted.

When the user enters 10 array elements as 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, then it gets stored in the array arr[] in the following way:

And the entered element, say 3, that has to be deleted, gets stored in elem. Now the dry run (inside the for loop) of the above program goes like this:

Note: In the above program, if an element is found at any index, say 2, in the array, then from here, all the elements get moved one index back. That is, an element from the third index is moved to the second, an element from the fourth index is moved to the third, and so on.

Delete an element and print a new array

This program has two extra features compared with the previous one. The two extra features are:

The question is: write a program in C++ that deletes an element and prints the new array. The answer to this question is given below:

#include<iostream>
using namespace std;
int main()
{
    int arr[100], tot, i, elem, j, found=0;
    cout<<"Enter the Size: ";
    cin>>tot;
    cout<<"Enter "<<tot<<" Array Elements: ";
    for(i=0; i<tot; i++)
        cin>>arr[i];
    cout<<"\nEnter Element to Delete: ";
    cin>>elem;
    for(i=0; i<tot; i++)
    {
        if(arr[i]==elem)
        {
            for(j=i; j<(tot-1); j++)
                arr[j] = arr[j+1];
            found=1;
            i--;
            tot--;
        }
    }
    if(found==0)
        cout<<"\nElement doesn't found in the Array!";
    else
    {
        cout<<"\nElement Deleted Successfully!";
        cout<<"\n\nThe New Array is:\n";
        for(i=0; i<tot; i++)
            cout<<arr[i]<<"  ";
    }
    cout<<endl;
    return 0;
}

Here is its sample run with user input.

First, enter the size as 8 and press the ENTER key. Here is the output you will see:

delete element in array c++

Enter all 8 array elements, namely 1, 2, 3, 4, 2, 5, and 2, and press the ENTER key.Here is the output you will see:

remove element from array c++

Now finally enter 2 as an element to remove or delete and press the ENTER key to delete 2 from the list and print the new array as shown in the snapshot given below:

c++ delete number from array

Using a function, delete an element from an array

This program is the complete version of deleting an element from an array in C++. It produces the output based on the input. At runtime, it also handles invalid user input. This program uses the user-defined function delElem().

The function delElem() takes three arguments. The first argument is array, the second is size, and the third is the element that has to be deleted. To learn more about function in C++, you can refer to its separate tutorial.

#include<iostream>
using namespace std;
int delElem(int [], int, int);
int main()
{
    int arr[100], tot, i, elem, chk;
    cout<<"Enter the Size: ";
    cin>>tot;
    if(tot>0)
    {
        cout<<"Enter "<<tot<<" Array Elements: ";
        for(i=0; i<tot; i++)
            cin>>arr[i];
        cout<<"\nEnter Element to Delete: ";
        cin>>elem;
        chk = delElem(arr, tot, elem);
        if(chk==101)
            cout<<"\nElement doesn't found in the Array!";
        else if(chk==102)
        {
            cout<<"\nElement Deleted Successfully!";
            cout<<"\n\nThe New Array is:\n";
            cout<<"Empty!";
        }
        else
        {
            cout<<"\nElement Deleted Successfully!";
            cout<<"\n\nThe New Array is:\n";
            for(i=0; i<chk; i++)
                cout<<arr[i]<<"  ";
        }
    }
    else
        cout<<"\nInvalid Input!";
    cout<<endl;
    return 0;
}
int delElem(int arr[], int tot, int elem)
{
    int i, j, found=0;
    for(i=0; i<tot; i++)
    {
        if(arr[i]==elem)
        {
            for(j=i; j<(tot-1); j++)
                arr[j] = arr[j+1];
            found++;
            i--;
            tot--;
        }
    }
    if(found==0)
        return 101;
    else if(tot==0)
        return 102;
    else
        return tot;
}

Here is its sample run with user input: 5 as size, 1, 2, 3, 2, 4 as 5 array elements, and 2 as an element to delete:

delete element from array using function c++

Here is another sample run with user input: 5 as size, 2, 2, 2, 2, 2 as 5 array elements, and 2 as an element to delete:

c++ delete element from array

Here is another sample run with user input: 5 as size, 6, 7, 8, 9, 10 as 5 array elements, and 3 as an element to delete:

c++ remove element from array

And if the user enters input, that is not a valid number for the above program. For example, a, -4, 0, # etc. The program will then display an error message such as "Invalid Input!"

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »


Liked this post? Share it!