Contact

Remove an Element from an Array in C++

When working with arrays in C++, it's common to encounter situations where you need to delete or remove elements from an array. Whether it's to efficiently manage memory or to update the array dynamically, understanding how to remove elements is a crucial skill for C++ developers. In this article, we will explore various methods to remove elements from an array, accompanied by example programs, their outputs, and explanations.

Method 1: Using Vector for Dynamic Array

One of the most flexible ways to remove an element from an array is by using the std::vector class in C++. Vectors provide dynamic resizing and convenient methods for element removal.

C++ Code
#include <iostream>
#include <vector>

int main() {
    std::vector<int> myArray = {1, 2, 3, 4, 5};

    // Removing element at index 2
    myArray.erase(myArray.begin() + 2);

    // Displaying the modified array
    for (int num : myArray) {
        std::cout << num << " ";
    }

    return 0;
}
Output
1 2 4 5

The erase method removes the element at the specified index (myArray.begin() + 2 in this case), effectively resizing the vector.

Method 2: Using std::remove_if for Static Array

For static arrays, the std::remove_if algorithm along with the std::begin and std::end functions can be employed to remove elements.

C++ Code
#include <iostream>
#include <algorithm>

int main() {
    int myArray[] = {1, 2, 3, 4, 5};
    int size = sizeof(myArray) / sizeof(myArray[0]);

    // Removing element with value 3
    int* newEnd = std::remove_if(std::begin(myArray), std::end(myArray), [](int num) {
        return num == 3;
    });

    // Displaying the modified array
    for (int* ptr = myArray; ptr != newEnd; ++ptr) {
        std::cout << *ptr << " ";
    }

    return 0;
}
Output
1 2 4 5

The remove_if algorithm shifts the elements to be removed to the end of the array, and the new end position is then used to display the modified array.

Method 3: Using std::copy_if for In-Place Removal

For in-place removal without changing the size of the array, the std::copy_if algorithm can be applied.

C++ Code
#include <iostream>
#include <algorithm>

int main() {
    int myArray[] = {1, 2, 3, 4, 5};
    int size = sizeof(myArray) / sizeof(myArray[0]);

    // Removing element with value 3
    auto newEnd = std::copy_if(std::begin(myArray), std::end(myArray), myArray, [](int num) {
        return num != 3;
    });

    // Displaying the modified array
    for (int* ptr = myArray; ptr != newEnd; ++ptr) {
        std::cout << *ptr << " ";
    }

    return 0;
}
Output
1 2 4 5

The copy_if algorithm is used to copy only the elements that satisfy the condition, effectively overwriting the original array with the modified content.

Method 4: Removing by User Input

In real-world scenarios, you might need to allow users to input the size of the array, the elements, and the element to remove. Here's an example program demonstrating user input for array manipulation.

C++ Code
#include <iostream>
#include <algorithm>

int main() {
    int size;

    // Getting the size of the array from the user
    std::cout << "Enter the size of the array: ";
    std::cin >> size;

    int myArray[size];

    // Getting elements from the user
    std::cout << "Enter " << size << " elements:\n";
    for (int i = 0; i < size; ++i) {
        std::cout << "Element " << i + 1 << ": ";
        std::cin >> myArray[i];
    }

    int elementToRemove;

    // Getting the element to remove
    std::cout << "Enter the element to remove: ";
    std::cin >> elementToRemove;

    // Removing the specified element
    auto newEnd = std::remove(std::begin(myArray), std::end(myArray), elementToRemove);

    // Displaying the modified array
    std::cout << "Modified array after removing " << elementToRemove << ":\n";
    for (int* ptr = myArray; ptr != newEnd; ++ptr) {
        std::cout << *ptr << " ";
    }

    return 0;
}
Output
Enter the size of the array: 5
Enter 5 elements:
Element 1: 10
Element 2: 20
Element 3: 30
Element 4: 20
Element 5: 40
Enter the element to remove: 20
Modified array after removing 20:
10 30 40

This program allows the user to specify the size of the array, input the elements, and then enter the element they want to remove. The std::remove algorithm is then used to remove the specified element.




Sharing is stylish! Gift your friends this awesome read!