Open In App

Remove All Occurrences of an Element in an Array

Last Updated : 09 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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));

Output
5

Time Complexity: O(n), where n is the number of elements in arr[]
Auxiliary Space: O(1)


Next Article

Similar Reads