Skip to content

Delete an element from an Array: Program and Algorithm

Delete an element from an Array

In this article, you’ll learn how to delete an element from a specific position of an array in C/C++, along with that you’ll also see the algorithm to delete an element from an array.

So, Let’s get started!

Prerequisite: You should have the knowledge of Traverse Operation in Array.

What is deletion operation in array?

Deletion operation in Array or delete an element from an Array simply means, Removing an existing element from a specific position of an array.

How do you delete an element from an array in C/C++?

Let’s see how you can delete an element from a specific position of an array step-by-step:

Variables we are using here:

  1. a : Name of the array.
  2. size : Size of the array (i.e., total number of elements in the array)
  3. i : Loop counter or counter variable for the for loop.
  4. pos : The position from where we wish to delete the element.

The following algorithm deletes a data element from a specific position pos (position specified by the programmer) of a linear array ‘a‘.

Algorithm to Delete an element from an Array:

  • Step 01: Start
  • Step 02: [Initialize counter variable. ] Set i = pos - 1
  • Step 03: Repeat Step 04 and 05 for i = pos - 1 to i < size
  • Step 04: [Move ith element backward (left). ] set a[i] = a[i+1]
  • Step 05: [Increase counter. ] Set i = i + 1
  • Step 06: [End of step 03 loop. ]
  • Step 07: [Reset size of the array. ] set size = size - 1
  • Step 08: Stop

In the above algorithm, step 2 to step 5 shifts (moves) each such element one location (position) backward (left) whose position is greater than the position of the element which we wish to delete.

The next step, Step 6, ends the loop.

And, at the last step, Step 7, decrements (reduces) the size of the array by one.

Now, few questions might be arising in your mind.

Why we are shifting (moving) each such element one position backward whose position is greater than the position of the element which we wish to delete?

And,

Why we are reducing the size of the array by one? Right?

The answer of the above two questions are very straightforward,

It’s only because, When you shift an element one position backward (left), the element that already present in that position (position where you are shifting the element) gets changed. And the value that you are shifting towards left gets copied or overwritten at that position.

Subsequently, after shifting all such values which are present right to the element which we wish to delete, When we reach to the position from where we have to delete the element then, we don’t shift the element which is present at that position or all such elements which are present left from that position to the left of it backward.

And, since we are shifting only those elements which are present ahead (right) from that position.

So at the end, the value of that position will change and the value next to it will come at that position. And in that case the already existing value will be removed.

As a result, the size of the array will now be reduced by one, due to the removal of one element from the array.

Visual Representation:

Delete an element from an array

Implementation in C:

// writing a program in C to delete an element from an array
 
void main()
{
     int i, size, pos;
     int a[] = {2, 4, 6, 8, 12};

     size = sizeof(a)/sizeof(a[0]);

     printf("The array elements before deletion operation:\n");

     for(i = 0; i < size; i++)
        printf("a[%d] = %d\n", i, a[i]);

     printf("\nEnter the position from where you wish to delete the element: ");
     scanf("%d", &pos);

     printf("\nThe array elements after deletion operation are:\n");

     for(i = pos - 1; i < size; i++)
        a[i] = a[i+1];

     size = size - 1;

     for(i = 0; i < size; i++)
        printf("a[%d] = %d\n", i, a[i]);

}

If you compile and run the above program, it will produce the following result:

Output:

The array elements before deletion operation:
a[0] = 2
a[1] = 4
a[2] = 6
a[3] = 8
a[4] = 12

Enter the position from where you wish to delete the element: 3

The array elements after deletion operation are:
a[0] = 2
a[1] = 4
a[2] = 8
a[3] = 12


Implementation in C++:

// writing a program in C++ to delete an element from an array

#include <iostream>
using namespace std;

int main()
{
     int i, size, x, pos;
     int a[] = {-1, 87, -68, 10, 8};

     size = sizeof(a)/sizeof(a[0]);

     cout << "The array elements before deletion operation:\n";

     for(i = 0; i < size; i++)
        cout << "a[" << i << "] = " << a[i] << "\n";

     cout << "\nEnter the position from where you wish to delete the element: ";
     cin >> pos;

     cout << "\nThe array elements after deletion operation are: ";

     for(i = pos - 1; i < size; i++)
        a[i] = a[i+1];

     size = size - 1;

     for(i = 0; i < size; i++)
        cout << "\na[" << i << "] = " << a[i];
     return 0;

}

If you compile and run the above program, it will produce the following result:

Output:

The array elements before deletion operation:
a[0] = -1
a[1] = 87
a[2] = -68
a[3] = 10
a[4] = 8

Enter the position from where you wish to delete the element: 2

The array elements after deletion operation are:
a[0] = -1
a[1] = -68
a[2] = 10
a[3] = 8


Conclusion:

In this article, you learned how to delete an element from a specific position of an array in C/C++, you also became familiar with the algorithm to delete an element from an array.

So,

I really hope that you liked my article and got a lot of value from it.

Now, It’s your turn!

If you have any question, feel free to ask in the comment section below right now.


Share this Post

Leave a Reply

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