Given an array and a target element, the task is to remove all occurrences of the target element while preserving the order of the remaining elements.
- All elements equal to the target are removed.
- The function can either create a new array or modify the original array in place.
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}
Approaches to Remove All Occurrences
The problem can be solved using an auxiliary array or by shifting the remaining elements within the original array.
Removing Elements Using an Auxiliary Array
In this approach, create a temporary array and copy only the elements that are different from the target.
Steps:
- Traverse the array and count the elements equal to the target.
- Create a temporary array with enough space for the remaining elements.
- Traverse the original array again and copy only the non-target elements.
- Print the resulting array.
#include <iostream>
using namespace std;
void removeAllOccurrences(int arr[], int target, int n)
{
int count = 0;
// Count occurrences of target
for (int i = 0; i < n; i++)
{
if (arr[i] == target)
count++;
}
int newSize = n - count;
int temp[newSize];
int index = 0;
// Copy non-target elements
for (int i = 0; i < n; i++)
{
if (arr[i] != target)
temp[index++] = arr[i];
}
// Print the resulting array
for (int i = 0; i < newSize; i++)
cout << temp[i] << " ";
}
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]);
removeAllOccurrences(arr, target, n);
return 0;
}
Output
1 4 6 8 9 10 7
1 4 6 8 9 10 7Explanation: The first traversal counts how many times the target occurs, which determines the size of the resulting array. The second traversal copies only the elements that are not equal to the target into temp[]. The original array remains unchanged.
Removing Elements Using In-Place Shifting
The array can also be modified without using an additional array. A separate counter keeps track of how many target elements have been encountered.
Steps:
- Traverse the array from left to right.
- If the current element is the target, increment count.
- Otherwise, move the element count positions to the left.
- The first n - count positions contain the resulting array.
#include <iostream>
using namespace std;
int removeAllOccurrences(int arr[], int target, int n)
{
int count = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] == target)
{
count++;
}
else
{
arr[i - count] = arr[i];
}
}
return n - count;
}
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]);
int newSize = removeAllOccurrences(arr, target, n);
for (int i = 0; i < newSize; i++)
cout << arr[i] << " ";
return 0;
}
Output
1 4 6 8 9 10 7
Explanation: The variable count stores the number of target elements encountered so far. Whenever a non-target element is found, it is shifted count positions to the left to fill the gaps created by the removed elements.