C++ Program to Delete an Element from an Array

In this article, you will learn and get code on deleting an element from an array in C++. The program is created in following ways:

  • Delete an Element from an Array
  • Delete an Element and Print New Array. Also allowed user to define array size
  • Using user-defined function

Delete Element from Array in C++

To delete an element from an array in C++ programming, you have to ask from user to enter the 10 array elements first. And then ask to enter the element that has to be delete. Now search that number or element and delete it, if found. Otherwise print a message like, element doesn't 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 build and run under 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, 10 as 10 array elements. After entering 10 elements for array arr[], enter the number/element say 3 that has to be delete from the list. Here is the final snapshot of the sample run:

delete element from array c++

Note - If the entered element (that has to be delete) found in repeated order (more than one time), then the element gets deleted from all the position. That is, repeated element also gets deleted.

When 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 following way:

  • arr[0]=1
  • arr[1]=2
  • arr[2]=3
  • arr[3]=4
  • arr[4]=5
  • arr[5]=6
  • arr[6]=7
  • arr[7]=8
  • arr[8]=9
  • arr[9]=10

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

  • Initial value, found=0, tot=10
  • At first evaluation of for loop, 0 gets initialized to i. The initialization (of for loop) happens first and only once. Now i=0
  • The condition i<tot or 0<10 evaluates to be true, therefore program flow goes inside the loop
  • And using if, the condition arr[i]==elem or arr[0]==3 or 1==3 evaluates to be false, therefore program flow does not goes inside if's body, rather it goes to the updatation part of for loop and increments the value of i. Now i=1
  • And again the condition i<tot or 1<10 evaluates to be true, therefore program flow again goes inside the loop, and evaluates the condition of if block
  • And again the condition arr[i]==elem or arr[1]==elem or 2==3 evaluates to be false, therefore program flow again increments i. Now i=2
  • Because the third time, condition of for loop evaluates to be true, therefore at third time also, program flow again goes inside the loop
  • But at this time, the condition of if, evaluates to be true, because element at index number 2 is 3 that equals the value stored in elem (element that has to be delete).
  • That is, the condition arr[i]==elem or arr[2]==3 or 3==3 evaluates to be true
  • Therefore program flow goes inside the if's body, and executes all of its statements
  • That is, using for loop, all the element from this (current or second) index gets shifted one index back.
    • That is at first, the value of i (2) gets initialized to j. So j=2
    • And the condition, j<(tot-1) or 2<(10-1) or 2<9 evaluates to be true, therefore program flow goes inside the loop and arr[j+1] or arr[2+1] or arr[3] or 4 (value at index number 3) gets initialized to arr[j] or arr[2]
    • Now the value of j gets incremented. So j=3
    • Again the condition, j<(tot-1) or 3<(10-1) or 3<9 evaluates to be true, therefore program flow again goes inside the loop and arr[j+1] or arr[3+1] or arr[4] or 5 gets initialized to arr[j] or arr[3]
    • This process continues, until the condition j<(tot-1) evaluates to be false
    • When the condition evaluates to be false, the evaluation of second for loop gets ended for now
    • And the value of found gets incremented. Its value is initialized with 0 at start of the program, to check whether it contain its value (0) or not, after evaluating the both for loops. If it contains its value as 0, therefore program flow never goes inside the if block. That means, no any element from the array gets matched to the element entered as element to be delete. And if its value will not be 0, then the element gets matched and deleted from that array
    • Now don't forgot to decrement the value of i by 1 to again start from the index where we came here in the if block, to compare with new element that has shifted to the index from where the element gets deleted previously
    • And the value of tot gets decremented, because one element gets deleted, then the size of array becomes 1 lesser than previous
  • Now the value of i gets incremented and again the condition of outer for loop gets evaluated.
  • This process continues, until the condition of outer for loop evaluates to be false
  • In this way, the element (along with repeated one) gets deleted from an array
  • After the execution of loop gets ended, just check the value of found and print the message accordingly

Note - In above program, if element found at any index say 2 in the array, then from here, all the elements gets moved to one index back. That is, element at 3rd index gets moved to 2nd index, element at 4th index gets moved to 3rd index, and so on

Delete Element and Print New Array

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

  • Allows user to define the size of array
  • Prints the new array after deleting the element

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

  • 8 as size
  • 1, 2, 3, 2, 4, 2, 5, 2 as 8 array elements
  • 2 as element to delete

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

delete element in array c++

Now enter all the 8 elements for the array say 1, 2, 3, 2, 4, 2, 5, 2 and press ENTER key. Here is the output you will see:

remove element from array c++

Now finally enter 2 as element to remove/delete and press 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

Delete Element from Array using Function

This program is the complete version of deleting an element from an array program in C++. It produces the output based on the input. Also handles an invalid input by user at run-time. This program uses user-defined function delElem().

The function delElem() takes three argument. The first argument is array, second is the size, and third is the element that has to be delete. 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 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 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 element to delete:

c++ remove element from array

And if user enters input, that is not a valid number for above program. For example, a, -4, 0, # etc. Then program will produce the output like Invalid Input!.

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »