C++ Program to delete an item from the array without using the library function


The purpose of an array is to provide access to the same type of data across several memory locations via base addresses and indexes. In a wide variety of applications, arrays are used to store data for a wide range of reasons. The array must efficiently handle adding, removing, and updating elements, just like other data structures. Both static and dynamic arrays include a number of library functions in C++ that handle various array-related operations. But in this article, we will see how to delete an element from an array without using any library functions in C++.

Understanding the concept with examples

Given array A = [89, 12, 32, 74, 14, 69, 45, 12, 99, 85, 63, 32]
After deleting an element from index 5, the array will be like this:

A = [89, 12, 32, 74, 14, 45, 12, 99, 85, 63, 32]

Deleting an element from any position, there are three possible cases. Deleting from the beginning, deleting from the end, and deleting from the middle of any index. Deleting from the end does not require any shifting. But the rest of the other two requires shifting elements towards the left. Firstly remove an element from that location then fill that place with successive elements. Let us see the algorithm and C++ code for a clear understanding.

Algorithm

  • Get the array A with n elements, the position pos

  • if pos >= n + 1, then

    • unable to delete, exit from the function

  • otherwise

    • for index c = pos to n − 1, do

      • A[ c ] = A[ c + 1 ]

    • end for

    • n := n − 1

  • end if

Example

#include <iostream>
#include <algorithm>
# define Z 30

using namespace std;

void displayArr(int arr[], int n ) {
   for( int i = 0; i < n; i++ ){
      cout << arr[ i ] << ", ";
   } 
   cout << endl;
} 

void deleteElement( int A[], int &n, int pos ){
   if ( pos >= n + 1 ) {   
      cout << "Deletion not possible" << endl;
      return;
   }
   
   else
   {   
      for ( int c = pos; c < n ; c++ ) {
         A[ c ] = A[ c + 1 ];          
      }
      n = n - 1;
   }   
}

int main() {
   int arr[ Z ] = {84, 56, 21, 32, 74, 96, 85, 41, 21, 94, 20, 37, 36, 75, 20};
   int n = 15;
   
   cout << "Given array elements: ";
   displayArr( arr, n);
   
   cout << "Delete from last position (position 15)" << endl;
   deleteElement( arr, n, 15 );
   cout << "Array after deleting last element: " << endl;
   displayArr( arr, n);
   
   cout << "Delete from first position (position 0)" << endl;
   deleteElement( arr, n, 0 );
   cout << "Array after deleting first element: " << endl;
   displayArr( arr, n);
   
   cout << "Delete from position 7" << endl;
   deleteElement( arr, n, 7 );
   cout << "Array after deleting element from index 7: " << endl;
   displayArr( arr, n);
}

Output

Given array elements: 84, 56, 21, 32, 74, 96, 85, 41, 21, 94, 20, 37, 36, 75, 20, 
Delete from last position (position 15)
Array after deleting last element: 
84, 56, 21, 32, 74, 96, 85, 41, 21, 94, 20, 37, 36, 75, 
Delete from first position (position 0)
Array after deleting first element: 
56, 21, 32, 74, 96, 85, 41, 21, 94, 20, 37, 36, 75, 
Delete from position 7
Array after deleting element from index 7: 
56, 21, 32, 74, 96, 85, 41, 94, 20, 37, 36, 75,

Conclusion

We have shown how to remove an element from an array in this article. This is a generic process that we may remove from anywhere we like, including the beginning, end, and middle. The vectors are not used because we are not using any library functions. For dynamic−sized arrays, vector-based methods are also an option.

Updated on: 13-Dec-2022

863 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements