Open In App

How to remove given object from an Array in Java?

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr of N objects, the task is to remove all the occurrences of a given object from the array in Java.

Example:

Input: String[] arr = { “Geeks”, “for”, “Geeks”, “hello”, “world” }, removeObj = “Geeks” 
Output: updated arr[] = {“for”, “hello”, “world”}
Explanation: All the occurrences of removeObj has been removed from the array.

Methods to remove objects from an array in Java are:

There are generally two methods to remove objects from an array in java, which are:

1. Using java.util.Arrays.copyOf method in Java:

java.util.Arrays.copyOf() method copies the given array to a specified length. We will use this method to remove all the occurrences of a given object from the array. The idea is to skip all the elements equal to the object to be removed (i.e., removeObj) and shift the rest of the objects to the left of the array. Then by using copyOf() method we will make the copy of the array to the last index where the last object not equal to removeObj has been shifted.

Below is the implementation for the above approach:

Java




// Java Program to remove a given
// object from the array
import java.util.Arrays;
 
public class Main {
    public static void main(String args[])
    {
 
        // Given an array of String objects
        String[] arr
            = { "Geeks", "for", "Geeks", "hello", "world" };
 
        // object to be removed
        String removeObj = "Geeks";
 
        // Here variable i is used to store
        // the element as ith index
        // variable j is iterated over
        // complete array to find the removeObj
        int i, j;
        for (i = 0, j = 0; j < arr.length; j++)
 
            // Check if jth object is
            // not equal to removeObj
            if (!arr[j].equals(removeObj)) {
 
                // If jth object is not equal to
                // removeObj then it is
                // inserted at ith index
                arr[i++] = arr[j];
            }
 
        // Making arr equal to copyof
        // of itself till ith index
        arr = Arrays.copyOf(arr, i);
 
        // Print the updated array
        System.out.println("Updated array:- ");
        for (int ind = 0; ind < arr.length; ind++) {
            System.out.println(arr[ind]);
        }
    }
}


Output

Updated array:- 
for
hello
world

Time Complexity: O(N), where N is length of array.
Auxiliary Space: O(1)

2. Using java.util.Arrays.asList() method in Java:

java.util.Arrays.asList() method is used to return a fixed-size list backed by the specified array. It makes a List out of the array. We will first convert the given array to List using Arrays.asList() method. Now the List interface in Java has a method as removeAll() to remove all the occurrences of the given element (i.e., removeObj) from the list. After that, we convert that list back to array by toArray() method. 

Below is the implementation:

Java




// Java Program to remove a
// given object from the array
import java.util.*;
 
public class Main {
    public static void main(String args[])
    {
 
        // Given an array of String objects
        String[] arr
            = { "Geeks", "for", "Geeks", "hello", "world" };
 
        // object to be removed
        String removeObj = "Geeks";
 
        // Converting the array to list
        List<String> list
            = new ArrayList<String>(Arrays.asList(arr));
 
        // Remove all occurrences of given string
        list.removeAll(Arrays.asList(removeObj));
 
        // Convert back list to array
        // the length of array
        // will also be updated
        arr = list.toArray(new String[0]);
 
        // Print the updated array
        System.out.println("Updated array:- ");
        for (int ind = 0; ind < arr.length; ind++) {
            System.out.println(arr[ind]);
        }
    }
}


Output

Updated array:- 
for
hello
world


Last Updated : 18 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads