Friday, December 27, 2024
Google search engine
HomeData Modelling & AIHow to remove given object from an Array in Java?

How to remove given object from an Array in Java?

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments