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 swap the element at kth index with element at ith 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++ 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) {
swap(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;
}
import java.util.ArrayList;
public class Main {
public static int removeElement(ArrayList<Integer> arr, int ele) {
int k = 0;
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i)!= ele) {
swap(arr, k, i);
k++;
}
}
return k;
}
public static void swap(ArrayList<Integer> arr, int i, int j) {
int temp = arr.get(i);
arr.set(i, arr.get(j));
arr.set(j, temp);
}
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(0);
arr.add(1);
arr.add(3);
arr.add(0);
arr.add(2);
arr.add(2);
arr.add(4);
arr.add(2);
int ele = 2;
System.out.println(removeElement(arr, ele));
}
}
from typing import List
def remove_element(arr: List[int], ele: int) -> int:
k = 0
for i in range(len(arr)):
if arr[i]!= ele:
arr[k], arr[i] = arr[i], arr[k]
k += 1
return k
def main():
arr = [0, 1, 3, 0, 2, 2, 4, 2]
ele = 2
print(remove_element(arr, ele))
if __name__ == "__main__":
main()
using System;
using System.Collections.Generic;
public class GFG
{
public static int RemoveElement(List<int> arr, int ele)
{
int k = 0;
for (int i = 0; i < arr.Count; i++)
{
if (arr[i]!= ele)
{
int e=arr[k];
arr[k] = arr[i];
arr[i] = e;
k++;
}
}
return k;
}
public static void Main()
{
List<int> arr = new List<int> { 0, 1, 3, 0, 2, 2, 4, 2 };
int ele = 2;
Console.WriteLine(RemoveElement(arr, ele));
}
}
function removeElement(arr, ele) {
let k = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i]!== ele) {
[arr[k], arr[i]] = [arr[i], arr[k]];
k += 1;
}
}
return k;
}
function main() {
let arr = [0, 1, 3, 0, 2, 2, 4, 2];
let ele = 2;
console.log(removeElement(arr, ele));
}
main();
Output
5
Time Complexity: O(n), where n is the number of elements in arr[]
Auxiliary Space: O(1)