How to remove given object from an Array in Java?
Last Updated :
18 Jan, 2023
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]);
}
}
}
OutputUpdated 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]);
}
}
}
OutputUpdated array:-
for
hello
world
Similar Reads
How to remove an element from ArrayList in Java? ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package. With
4 min read
How to remove Objects from Associative Array in JavaScript ? In this article, we are going to learn about removing Objects from Associative Array in Javascript, In JavaScript, you can remove objects from an associative array (also known as an object) using the following methods. Table of Content Using JavaScript delete operatorUsing JavaScript Array.filter()
4 min read
How to Remove an Entry by Key in JavaScript Object? In JavaScript, objects store data in the form of key-value pairs where the key may be any property of the object. In this article let us see how to remove key-value pairs a by given key in the object.Table of ContentUsing the delete operatorUsing the filter() methodUsing Destructuring and Object.ass
3 min read
JavaScript - How to Remove an Element from an Array? Removing elements from an array is a fundamental operation in JavaScript, essential for data manipulation, filtering, and transformation. This guide will explore different methods to efficiently remove elements from an array, enhancing your understanding and capability in handling arrays.1. Using po
3 min read
How To Remove Specific JSON Object From Array JavaScript? Removing Specific JSON objects from the array is important because it allows for targeted data manipulation and maintenance within JavaScript applications. we will learn to Remove Specific JSON Object From Array JavaScript. These are the following methods: Table of Content Using filter() methodUsing
3 min read
Remove all elements from the ArrayList in Java Prerequisite: ArrayList in Java Given an ArrayList, the task is to remove all elements of the ArrayList in Java. Examples: Input: ArrayList = [1, 2, 3, 4] Output: ArrayList = [] Input: ArrayList = [12, 23, 34, 45, 57, 67, 89] Output: ArrayList = [] Using clear() method: Syntax: collection_name.clear
2 min read