A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/remove-all-occurrences-of-an-element-from-array-in-java/ below:

Remove all Occurrences of an Element from Array in Java

Remove all Occurrences of an Element from Array in Java

Last Updated : 11 Jul, 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)); 
    } 
} 

Output
[9, 2, 1, 7, 2, 5]

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

Output
[9, 2, 1, 7, 2, 5]
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));
    }
}

Output
[9, 2, 1, 7, 2, 5]
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));
    }
}

Output
[9, 2, 1, 7, 2, 5]
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));
    }
}

Output
[9, 2, 1, 7, 2, 5]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4