Remove all Occurrences of an Element from Array in Java (original) (raw)

Last Updated : 01 May, 2025

In Java, removing all occurences of a given element from an array can be done using different approaches that are,

**Problem Stament: Given an **array and a **key, the task is to **remove all occurrences of the specified key from the array in Java.

**Examples to Remove Elements Occurrences in Array:

**Input: array = { 3, 9, 2, 3, 1, 7, 2, 3, 5 }, key = 3
**Output: [9, 2, 1, 7, 2, 5]

**Input: array = { 10, 20, 10, 30, 50, 10 }, key = 10
**Output: [20, 30, 50]

**Illustration: In this example, we will remove all occurrences of an element from an array using the Arrays.copyOf method.

Java `

// Java program remove all occurrences // of an element from Array using naive method import java.util.Arrays;

class Geeks {

// function to remove all occurrences 
// of an element from an array 
public static int[] removeElements(int[] a, int k) 
{ 
    // Move all other elements to beginning 
    int ind = 0; 
    
    for (int i=0; i<a.length; i++) 
        if (a[i] != k) 
            a[ind++] = a[i]; 

    // Create a copy of arr[] 
    return Arrays.copyOf(a, ind); 
} 

// Driver code 
public static void main(String[] args) 
{ 
    int[] a = { 3, 9, 2, 3, 1, 7, 2, 3, 5 }; 
    int k = 3; 
    a = removeElements(a, k); 
    System.out.println(Arrays.toString(a)); 
} 

}

`

**Complexity of the above method:

Java Program to Remove All Occurrences of an Element from Array

1. Using Java 8 Stream

**Approach:

**Illustration: Here, we will use the Java 8 streams to remove all occurrences of an element from an array.

Java `

// Java program remove all occurrences // of an element from Array using Stream import java.util.Arrays;

public class Geeks { public static void main(String[] args) {

    // Example array
    Integer[] a = { 3, 9, 2, 3, 1, 7, 2, 3, 5 }; 

    // Element to be removed
    int k = 3;

    // Remove all occurrences of the element using Java 8 Stream
    Integer[] ans = Arrays.stream(a)
    
            // Filter out the element to remove
            .filter(e -> e != k)  
            .toArray(Integer[]::new);           

    System.out.println(Arrays.toString(ans));
}

}

`

2. Using Java ArrayList

Approach:

Alternative Approach:

**Illustration: Using the ArrayList to remove all occurrences of an element from an array.

Java `

// Java program remove all occurrences // of an element from Array using ArrayList import java.util.ArrayList; import java.util.Arrays; import java.util.List;

// Driver Class public class Geeks {

  // Main Method
public static void main(String[] args) {
    
    Integer[] a = { 3, 9, 2, 3, 1, 7, 2, 3, 5 }; 

    // Element to be removed
    Integer k = 3;

    // Convert array to ArrayList
    List<Integer> l = new ArrayList<>(Arrays.asList(a));

    // Remove all occurrences of the element
    l.removeAll(Arrays.asList(k));

    // Convert ArrayList back to array
    Integer[] newArr = l.toArray(new Integer[0]);

    // Print the result
    System.out.println(Arrays.toString(newArr));
}

}

`

3. Using List.removeAll()

**Approach:

**Illustration: Using the List.removeAll() to remove all occurrences of an element from an array.

Java `

// Java program remove all occurrences // of an element from Array using List.removeAll import java.util.*;

// Driver Class public class Geeks { // Main Method public static void main(String[] args) {

    // Example array
    Integer[] a = { 3, 9, 2, 3, 1, 7, 2, 3, 5 };

    // Element to be removed
    Integer k = 3;

    // Convert array to List (using ArrayList to allow modification)
    List<Integer> l = new ArrayList<>(Arrays.asList(a));

    // Remove all occurrences of the element
    l.removeAll(Arrays.asList(k));

    // Convert List back to array
    Integer[] ans = l.toArray(new Integer[0]);

    // Print the result
    System.out.println(Arrays.toString(ans));
}

}

`

4. Using List.removeIf()

**Approach:

**Illustration: Using the List.removeIf() to remove all occurrences of an element from an array.

Java `

// Java program remove all occurrences // of an element from Array using removeIf() import java.util.*;

// Driver Class public class Geeks { // Main Method public static void main(String[] args) {

    // Example array
    Integer[] a = { 3, 9, 2, 3, 1, 7, 2, 3, 5 };

    // Element to be removed
    int k = 3;

    // Convert array to List (using ArrayList to allow modification)
    List<Integer> l = new ArrayList<>(Arrays.asList(a));

    // Remove all occurrences using removeIf()
    l.removeIf(e -> e == k);

    // Convert List back to array
    Integer[] ans = l.toArray(new Integer[0]);

    System.out.println(Arrays.toString(ans));
}

}

`

Similar Reads

Java Basic Programs





















Java Pattern Programs














Java Conversion Programs















Java Classes and Object Programs
















Java Methods Programs











Java Searching Programs




Java 1-D Array Programs













Java 2-D Arrays (Matrix) Programs













Java String Programs