Remove All Occurrences of an Element in an Array
Last Updated :
09 Nov, 2024
Given an integer array arr[] and an integer ele the task is to the remove all occurrences of ele from arr[] in-place and return the number of elements which are not equal to ele. If there are k number of elements which are not equal to ele then the input array arr[] should be modified such that the first k elements should contain the elements which are not equal to ele and then the remaining elements.
Note: The order of first k elements may be changed.
Examples:
Input: arr[] = [3, 2, 2, 3], ele = 3
Output: 2
Explanation: The answer is 2 because there are 2 elements which are not equal to 3 and arr[] will be modified such that the first 2 elements contain the elements which are not equal to 3 and remaining elements can contain any element. So, modified arr[] = [2, 2, _, _]
Input: arr[] = [0, 1, 3, 0, 2, 2, 4, 2], ele = 2
Output: 5
Explanation: The answer is 5 because there are 5 elements which are not equal to 2 and arr[] will be modified such that the first 5 elements contain the elements which are not equal to 2 and remaining elements can contain any element. So, modified arr[] = [0, 1, 3, 0, 4, _, _, _]
Approach:
The idea is to iterate over the array while maintaining a subarray at the beginning that contains only the elements which are not equal to ele. So, we can use a counter, say k to track the ending point of this subarray and whenever we encounter an element which is not equal to ele, we can add the element at kth index and increment the value of k.
Step-by-step algorithm:
- Initialize j to 0. This will track the count of the elements not equal to ele.
- Iterate over each element in the array using the loop with the index i.
- If arr[i] is not equal to the ele, set arr[j] = arr[i] and increment j.
- Return j.
C++
// C++ Code to remove all occurrences of
// an element in an array
#include <iostream>
#include <vector>
using namespace std;
int removeElement(vector<int>& arr, int ele) {
// Initialize the counter for the
// elements not equal to ele
int k = 0;
for (int i = 0; i < arr.size(); i++) {
// Place the element which is not
// equal to ele at the kth position
if (arr[i] != ele) {
arr[k] = arr[i];
// Increment the count of
// elements not equal to ele
k++;
}
}
return k;
}
int main() {
vector<int> arr = {0, 1, 3, 0, 2, 2, 4, 2};
int ele = 2;
cout << removeElement(arr, ele) << endl;
return 0;
}
C
// C Code to remove all occurrences of
// an element in an array
#include <stdio.h>
int removeElement(int arr[], int n, int ele) {
// Initialize the counter for the
// elements not equal to ele
int k = 0;
for (int i = 0; i < n; i++) {
// Place the element which is not
// equal to ele at the kth position
if (arr[i] != ele) {
arr[k] = arr[i];
// Increment the count of
// elements not equal to ele
k++;
}
}
return k;
}
int main() {
int arr[] = {0, 1, 3, 0, 2, 2, 4, 2};
int n = sizeof(arr) / sizeof(arr[0]);
int ele = 2;
printf("%d\n", removeElement(arr, n, ele));
return 0;
}
Java
// Java Code to remove all occurrences of
// an element in an array
import java.util.Arrays;
class GfG {
static int removeElement(int[] arr, int ele) {
// Initialize the counter for the
// elements not equal to ele
int k = 0;
for (int i = 0; i < arr.length; i++) {
// Place the element which is not
// equal to ele at the kth position
if (arr[i] != ele) {
arr[k] = arr[i];
// Increment the count of
// elements not equal to ele
k++;
}
}
return k;
}
public static void main(String[] args) {
int[] arr = {0, 1, 3, 0, 2, 2, 4, 2};
int ele = 2;
System.out.println(removeElement(arr, ele));
}
}
Python
# Python Code to remove all occurrences of
# an element in an array
def removeElement(arr, ele):
# Initialize the counter for the
# elements not equal to ele
k = 0
for i in range(len(arr)):
# Place the element which is not
# equal to ele at the kth position
if arr[i] != ele:
arr[k] = arr[i]
# Increment the count of
# elements not equal to ele
k += 1
return k
if __name__ == "__main__":
arr = [0, 1, 3, 0, 2, 2, 4, 2]
ele = 2
print(removeElement(arr, ele))
C#
// C# Code to remove all occurrences of
// an element in an array
using System;
class GfG {
static int removeElement(int[] arr, int ele) {
// Initialize the counter for the
// elements not equal to ele
int k = 0;
for (int i = 0; i < arr.Length; i++) {
// Place the element which is not
// equal to ele at the kth position
if (arr[i] != ele) {
arr[k] = arr[i];
// Increment the count of
// elements not equal to ele
k++;
}
}
return k;
}
static void Main() {
int[] arr = { 0, 1, 3, 0, 2, 2, 4, 2 };
int ele = 2;
Console.WriteLine(removeElement(arr, ele));
}
}
JavaScript
// JavaScript Code to remove all occurrences of
// an element in an array
function removeElement(arr, ele) {
// Initialize the counter for the
// elements not equal to ele
let k = 0;
for (let i = 0; i < arr.length; i++) {
// Place the element which is not
// equal to ele at the kth position
if (arr[i] !== ele) {
arr[k] = arr[i];
// Increment the count of
// elements not equal to ele
k++;
}
}
return k;
}
// Driver Code
const arr = [0, 1, 3, 0, 2, 2, 4, 2];
const ele = 2;
console.log(removeElement(arr, ele));
Time Complexity: O(n), where n is the number of elements in arr[]
Auxiliary Space: O(1)
Similar Reads
Delete First Occurrence of Given Element from an Array Given an array of integers, the task is to delete a given element from the array. If there are multiple occurrences of the element, we need to remove only its first occurrence.Examples:Input: arr[] = [10, 20, 30, 40], ele = 20Output: [10, 30, 40]Input: arr[] = [10, 20, 30, 40], ele = 25Output: [10,
8 min read
Remove First Element from an Array in PHP Given an array, the task is to remove the first element from an array in PHP. Examples:Input: arr = [1, 2, 3, 4, 5, 6, 7]; Output: 2, 3, 4, 5, 6, 7 Input: arr = [3, 4, 5, 6, 7, 1, 2] Output: 4, 5, 6, 7, 1, 2Below are the methods to remove the first element from an array in PHP:Table of ContentUsing
3 min read
JavaScript - How to Remove an Element from an Array? Removing elements from an array is a fundamental operation in JavaScript, essential for data manipulation, filtering, and transformation. This guide will explore different methods to efficiently remove elements from an array, enhancing your understanding and capability in handling arrays.1. Using po
3 min read
Delete all odd frequency elements from an Array Given an array arr containing integers of size N, the task is to delete all the elements from the array that have odd frequencies.Examples: Input: arr[] = {3, 3, 3, 2, 2, 4, 7, 7} Output: {2, 2, 7, 7} Explanation: Frequency of 3 = 3 Frequency of 2 = 2 Frequency of 4 = 1 Frequency of 7 = 2 Therefore,
5 min read
Removing Array Element and Re-Indexing in PHP In order to remove an element from an array, we can use unset() function which removes the element from an array, and then use array_values() function which indexes the array numerically automatically. Function Usedunset(): This function unsets a given variable. Syntax:void unset ( mixed $var [, mix
2 min read