How to Remove an Element from Array in C++?
Last Updated :
11 Oct, 2024
In C++, removing an element from an array is tricky because we cannot modify the memory space occupied by the array. So, we pseudo delete the element by moving it to the end of the array and reducing the size. In this article, we will learn how to remove a value from an array in C++.
Examples
Input: arr[] = {3, 1, 2, 5, 90}, val = 2
Output: 3 1 5 90
Explanation: As element 2 is removed from an array.
Input: arr[] = {3, 1, 2, 5, 90}, val = 90
Output: 3, 1, 2, 5
Explanation: As element 90 is removed from an array.
There are 2 primary ways to remove the elements using remove function in C++:
Remove Element Using Library Function
C++ STL provides std::remove() method using which we can move the elements to be removed to the end of the array and update the variable that keeps the track of the size of the array (as we cannot remove the memory occupied by fixed size array elements).
Syntax
std::remove(first, last, key)
where,
- first: Iterator to the first element of the range.
- last: Iterator to the element just after the last element of the given range.
- key: Element to be removed.
std::remove() function returns an iterator to new logical end of the array that contains only those elements that are not removed.
Example
C++
// C++ Program to remove an element from the
// array using std::remove()
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[] = {3, 1, 2, 5, 90};
int n = sizeof(arr) / sizeof(arr[0]);
// The element which we have to delete
int key = 2;
// Use std::remove() to move all occurrences
// of 2 to the end
auto end = std::remove(arr, arr + n, key);
// Calculate the new size of the array
// after removal
n = end - arr;
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Time Complexity: O(n), where n is the number of elements in the array.
Auxiliary Space: O(1)
Remove Element Manually Using Loop
To remove a value from an array in C++, we first search the position of the element to be removed, then move elements that are on the right side of this element to one position towards the front (i.e. towards the left). Then decrement the array size variable. This effectively deletes the from the array.
Code Implementation
C++
// C++ program to illustrate how to remove a
// given element from an array
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[] = {3, 1, 2, 5, 90};
int n = sizeof(arr) / sizeof(arr[0]);
// The element which we have to delete
int key = 2;
// Search key in array
for (int i = 0; i < n; i++) {
// If key is found in array
if (arr[i] == key) {
// Reduce size of array and move all
// elements one space towards left
n = n - 1;
for (int j = i; j < n; j++)
arr[j] = arr[j + 1];
break;
}
}
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Time Complexity: O(n), where n is the number of elements in the array.
Auxiliary Space: O(1)
Similar Reads
How to Remove an Element from a Set in C++? In C++, sets are a type of associative container in which each element has to be unique. The values are stored in a specific sorted order i.e. either ascending or descending. In this article, we will see how to remove specific elements from a set in C++. Example Input: set = {100,120,12,56,78,9,32,4
2 min read
How to Remove an Element from a List in C++? In C++, the STL provides a std::list container that represents a doubly linked list to store the sequential data in non-contiguous memory locations. In this article, we will learn how to remove an element from a list in C++. Example: Input: myList = {1, 2, 3, 4, 5, 6, 7, 8} Target = 5 Output: // rem
2 min read
How to Remove an Element from a Deque in C++? In C++ STL, a container called deque (known as a double-ended queue) allows us to insert and delete elements at both its beginning and its end. In this article, we will learn how to remove a specific element from a deque in C++ STL. Example: Input: myDeque= {4, 2, 3, 5, 2} Target = 4 Output: Deque A
2 min read
How to Remove an Element from Vector in C++? In this article, we will learn how to remove a given element from the vector in C++.The most straightforward method to delete a particular element from a vector is to use the vector erase() method. Let's look at a simple example that shows how to use this function:C++#include <bits/stdc++.h> u
2 min read
How to Delete an Element from an Array in C++? In C++, arrays are data structures that allow users to store data of the same type in contiguous memory locations. In this article, we will learn how to delete an element from an array in C++. Example: Input:myarr = {20, 5, 1, 10, 15};Target: 1Output:Array after deletion: 20 5 10 15Remove an Element
3 min read