Remove all occurrences of an element from Array in Java

21 Jun, 2022
Given an array and a key, the task is to remove all occurrences of the specified key from the array in Java. Examples:
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]
JAVA
// Java program remove all occurrences// of an element from Array using naive method import java.util.Arrays; class GFG {     // function to remove all occurrences    // of an element from an array    public static int[] removeElements(int[] arr, int key)    {        // Move all other elements to beginning        int index = 0;        for (int i=0; i<arr.length; i++)            if (arr[i] != key)                arr[index++] = arr[i];         // Create a copy of arr[]        return Arrays.copyOf(arr, index);    }     // Driver code    public static void main(String[] args)    {        int[] array = { 3, 9, 2, 3, 1, 7, 2, 3, 5 };        int key = 3;        array = removeElements(array, key);        System.out.println(Arrays.toString(array));    }}
[9, 2, 1, 7, 2, 5]
Time Complexity: O(n)
Space Complexity: O(n)
Get the array and the key.Filter all element of the list which is equal to a given keyConvert the list back to an array and return it.
Get the array and the key.
Filter all element of the list which is equal to a given key
Convert the list back to an array and return it.
Get the array and the key.Create an empty ArrayListInsert all elements from the array into the list except the specified keyConvert the list back to an array and return it.
Get the array and the key.
Create an empty ArrayList
Insert all elements from the array into the list except the specified key
Convert the list back to an array and return it.
First Create a List of Array.Remove all elements of the array into the list that are the specified key.Convert the list back to an array and return it.
First Create a List of Array.
Remove all elements of the array into the list that are the specified key.
Convert the list back to an array and return it.
First Create an empty List of Array.Insert all elements of the array into the listRemove all elements which is you want to removeConvert the list back to an array and return it.
First Create an empty List of Array.
Insert all elements of the array into the list
Remove all elements which is you want to remove
Convert the list back to an array and return it.
First Create an empty List of Array.Insert all elements of the array into the listRemove all those element which is you want to remove using the equals() methodConvert the list back to an array and return it.
First Create an empty List of Array.
Insert all elements of the array into the list
Remove all those element which is you want to remove using the equals() method
Convert the list back to an array and return it.
harshmaster07705
Java-Array-Programs
Java-ArrayList
Java-Arrays
Java-Collections
Technical Scripter 2018
Java
Technical Scripter
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java