C++ Program to Delete an Element from Array
Here you will get and learn the example code of C++ program to delete an element from array list. This is an example of single dimensional array to delete a specific element from the array list.
Example Program to Delete an Element from Array in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include<iostream> using namespace std; int main() { int i, ar[50],ar1[50],n,item; cout<<"\nEnter the size of the array : "; cin>>n; cout<<"\nEnter elements of array : "; for(i=0;i<=(n-1);i++) { cin>>ar[i]; } cout<<"\nEnter element to delete : "; cin>>item; i=0; int k=0; while(i<n) { if (ar[i]==item ) { i++; } else { ar1[k]=ar[i]; k++; i++; } } cout<<"\n\nArray elements are : "; for(i=0;i<n-1;i++) { cout<<" "<<ar1[i]; } return 0; } |
Output
Enter the size of the array : 7
Enter elements of array : 1 2 3 4 5 6 7
Enter element to delete : 5
Array elements are : 1 2 3 4 6 7
Check out our other C++ programming Examples