How to Delete an Element from an Array in C++?
Last Updated :
18 Mar, 2024
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: 1
Output:
Array after deletion: 20 5 10 15
Remove an Element from an Array in C++
In C++, arrays are static, which means we cannot change their size once declared. Therefore, technically, we cannot delete an element from an array. However, we can mimic this behavior to delete element from the array in C++ using the below approach:
Approach
- Find the index of the element you want to delete from the array.
- Shift all the elements after that index to the left.
- Update the size of the array after deletion if you are keeping it.
C++ Program to Delete an Element from an Array
The following program illustrates how to delete an element from an array in C++.
C++
// C++ Program to illustrate how to delete an element from
// an array
#include <algorithm>
#include <iostream>
using namespace std;
// Function to delete an element from an array
void deleteElement(int arr[], int& size, int element)
{
int indexToDelete = -1;
// Find the index of the element to delete
int* ptr = find(arr, arr + size, element);
indexToDelete = ptr - arr;
// Print error if the element to delete was not found in
// the array
if (indexToDelete > size - 1) {
cout << "Element not found in the array." << endl;
return;
}
// Shift elements to the left starting from the index to
// be deleted
for (int i = indexToDelete; i < size - 1; ++i) {
arr[i] = arr[i + 1];
}
// Update the size of the array
--size;
}
// driver code
int main()
{
// Initialize an array
int arr[] = { 20, 5, 1, 10, 15 };
// Find the size of the array
int size = sizeof(arr) / sizeof(arr[0]);
// Print the array before deletion
cout << "Array before deletion:";
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
// Declare the element you want to delete from the array
int target = 1;
// Call the deleteElement function to delete the element
deleteElement(arr, size, target);
// Print the updated array after deletion
cout << "Array after deletion:";
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
return 0;
}
OutputArray before deletion:20 5 1 10 15
Array after deletion:20 5 10 15
Time Complexity: O(N), where N is the size of the array.
Auxiliary Space: O(1)
Similar Reads
How to Delete an Element from a Set in C++? A set in C++ is a container that stores unique elements in a sorted order. In this article, we will learn how to delete a specific element from a set. Example: Input: mySet = {5, 2, 8, 1, 4} Element to delete: 2 Output: mySet = {5, 1, 8, 4}Delete an Element from a Set in C++To delete a specific elem
2 min read
How to Delete an Element from a Multiset in C++? In C++, a multiset is a container that stores elements in a specific order. Multiple elements can have the same values. In this article, we will learn how to delete a specific element from a multiset. Example: Input: myMultiset = {5, 2, 8, 5, 8, 8}; Element to delete: 8 Output: myMultiset = {5, 2, 5
2 min read
How to Remove an Element from Array in C++? 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++.ExamplesInput: a
3 min read
How to Delete All Elements from a Vector in C++? In C++, you can delete all items from a vector to either clear its contents or free its memory. In this article, we will learn how to delete all items from a vector in C++.The recommended way to delete all items from a vector is by using the vector clear() function. Letâs take a look at a simple exa
2 min read
How to Delete an Element from the Set in C++? In C++, the set container stores unique elements in the sorted order and the deletion operation should not disrupt this sorted order.C++ provides a built-in function set erase(), which can be used to easily delete the given element by passing its value to the function. Let's take a look at the simpl
1 min read