DEV Community

Cover image for How to Delete an Element from an Array in C++
Mujahida Joynab
Mujahida Joynab

Posted on

How to Delete an Element from an Array in C++

Code to delete an element from an array in C++


#include <bits/stdc++.h>
using namespace std;
int a[100000];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    int index;
    cin >> index;

    n--;
    for (int i = index; i <= n ; i++)
    {
        a[i] = a[i + 1];
    }

    for (int i = 1; i <= n; i++)
    {

        cout << a[i] << " ";
    }
}


Enter fullscreen mode Exit fullscreen mode

Top comments (0)