C++ Program to Remove All Occurrences of an Element in an Array
Last Updated :
08 Jul, 2022
Here we will see how to remove all the occurrences of an element in an array using a C++ program. Below are the examples:
Input: {1, 4, 3, 6, 8, 3, 9, 10, 3, 3, 7}
target=3
Output: {1, 4, 6, 8, 9, 10, 7}
Input: {12, 11, 10, 17, 12, 4, 7, 12}
target=12
Output: {11, 10, 17, 4, 7}
There are 2 ways to remove all the occurrences of an element in an array in C++:
- Brute Force Approach.
- Optimized Approach (In-place Shifting).
Let’s start discussing each of these methods in detail.
1. Brute-force Approach
In this method, create another array and copy all elements except the target element. Below is the C++ program to remove all the occurrences of an element in an array using a brute-force approach:
C++
#include <iostream>
using namespace std;
void remove_all_occurrence( int arr[],
int target,
int n)
{
int cnt = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] == target)
cnt++;
}
int new_arr[n - cnt];
int ind = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] != target)
{
new_arr[ind] = arr[i];
ind++;
}
}
int m = ( sizeof (new_arr) /
sizeof (new_arr[0]));
for ( int i = 0; i < m; i++)
{
cout << new_arr[i] << " " ;
}
return ;
}
int main()
{
int arr[]={1, 4, 3, 6, 8,
3, 9, 10, 3, 3, 7};
int target = 3;
int n = ( sizeof (arr) /
sizeof (arr[0]));
remove_all_occurrence(arr, target, n);
return 0;
}
|
Time Complexity: O(n)
Space Complexity: O(n)
2. Optimized Approach (In-place Shifting)
In this method, shift the non-target element to the left side.
- Check whether the current element is the target element or not.
- If it is the target element increase the cnt variable.
- After this element, all the non-target elements will shift left with the gap of (n-cnt).
Below is the C++ program to remove all occurrences of an element from an array using an optimized approach:
C++
#include <iostream>
using namespace std;
void remove_all_occurrence( int arr[],
int target,
int n)
{
int cnt = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] != target)
{
arr[i - cnt] = arr[i];
}
else
{
cnt++;
}
}
for ( int i = 0; i < (n - cnt); i++)
{
cout << arr[i] << " " ;
}
return ;
}
int main()
{
int arr[] = {1, 4, 3, 6, 8, 3,
9, 10, 3, 3, 7};
int target = 3;
int n = ( sizeof (arr) /
sizeof (arr[0]));
remove_all_occurrence(arr, target, n);
return 0;
}
|
- Time Complexity: O(n)
- Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...