Sunday, June 5, 2022
HomeWordPress DevelopmentFind out how to take away given object from an Array in...

Find out how to take away given object from an Array in Java?


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

Instance:

Enter: String[] arr = { “Geeks”, “for”, “Geeks”, “good day”, “world” }, removeObj = “Geeks” 
Output: up to date arr[] = {“for”, “good day”, “world”}
Expalanation: All of the occurrences of removeObj has been faraway from the array.

Strategies to take away objects from an array in Java are:

There are usually two strategies to take away objects from an array in java, that are:

1. Utilizing java.util.Arrays.copyOf methodology in Java:

java.util.Arrays.copyOf() methodology copies the given array to a specified size. We’ll use this methodology to take away all of the occurrences of a given object from the array. The thought is to skip all the weather equal to the article to be eliminated (i.e., removeObj) and shift the remainder of the objects to the left of the array. Then by utilizing copyOf() methodology we’ll make the copy of the array to the final index the place the final object not equal to removeObj has been shifted.

Beneath is the implementation for the above strategy:

Java

import java.util.Arrays;

  

public class Predominant {

    public static void fundamental(String args[])

    {

  

        

        String[] arr

            = { "Geeks", "for", "Geeks", "good day", "world" };

  

        

        String removeObj = "Geeks";

  

        

        

        

        

        int i, j;

        for (i = 0, j = 0; j < arr.size; j++)

  

            

            

            if (!arr[j].equals(removeObj)) {

  

                

                

                

                arr[i++] = arr[j];

            }

  

        

        

        arr = Arrays.copyOf(arr, i);

  

        

        System.out.println("Up to date array:- ");

        for (int ind = 0; ind < arr.size; ind++) {

            System.out.println(arr[ind]);

        }

    }

}

Output

Up to date array:- 
for
good day
world

2. Utilizing java.util.Arrays.asList() methodology in Java:

java.util.Arrays.asList() methodology is used to return a fixed-size listing backed by the required array. It makes a Record out of the array. We’ll first convert the given array to Record utilizing Arrays.asList() methodology. Now the Record interface in Java has a way as removeAll() to take away all of the occurrences of the given aspect (i.e., removeObj) from the listing. After that, we convert that listing again to array by toArray() methodology. 

Beneath is the implementation:

Java

import java.util.*;

  

public class Predominant {

    public static void fundamental(String args[])

    {

  

        

        String[] arr

            = { "Geeks", "for", "Geeks", "good day", "world" };

  

        

        String removeObj = "Geeks";

  

        

        Record<String> listing

            = new ArrayList<String>(Arrays.asList(arr));

  

        

        listing.removeAll(Arrays.asList(removeObj));

  

        

        

        

        arr = listing.toArray(new String[0]);

  

        

        System.out.println("Up to date array:- ");

        for (int ind = 0; ind < arr.size; ind++) {

            System.out.println(arr[ind]);

        }

    }

}

Output

Up to date array:- 
for
good day
world

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments