How to remove array elements that are equal in value?

Using ArrayUtills in java code, you can remove an element from the java array. Below is the code that deletes an element in a separate index (in code "2", which deletes an element value equal to "10".

import java.util.Arrays;

import org.apache.commons.lang3.ArrayUtils;


public class RemoveObjectFromArray{

 public static void main(String args[]) {


     int[] test = new int[] { 10, 13, 10, 10, 105};

     System.out.println("Original Array : size : " + test.length );
     System.out.println("Contents : " + Arrays.toString(test));

     //let remove or delete an element from Array using Apache Commons ArrayUtils
     test = ArrayUtils.remove(test, 2); //removing element at index 2

     //Size of array must be 1 less than original array after deleting an element
     System.out.println("Size of array after removing an element  : " + test.length);
     System.out.println("Content of Array after removing an object : "
                       + Arrays.toString(test));

 } }

It outputs as:

run:
Original Array : size : 5
Contents : [10, 13, 10, 10, 105]
Size of array after removing an element  : 4
Content of Array after removing an object : [10, 13, 10, 105]

How can I change the code to get the following output:

run:
Original Array : size : 5
Contents : [10, 13, 10, 10, 105]
Size of array after removing an element  : 2
Content of Array after removing an object : [ 13, 105]
+4
source share
5 answers

Try this code

while (ArrayUtils.contains(test, 10))  {
  //let remove or delete an element from Array using Apache Commons ArrayUtils
  test = ArrayUtils.removeElement(test, 10); //removing element with value 10
}

It should solve your problem.

+3
source

I would use an ArrayList in your case, but if you want to stick with your array, you can do the following:

int count = 0;
for(int i = 0; i < test.length; i++){
  if (test[i] == 10) {
    count++;
  }
 }
 int[] newTest = new int[count];
 count = 0;
 for( int = 0; i < test.length; i++){
   if(test[i] != 10){
     newTest[count++] = test[i];
   }
  }

I have not tested it

+2
source

, , - :

public static void removeDupes(int[] array) {

    int[] forbiddenNumbers = new int[array.length];

    Arrays.sort(array);

    for(int number : array) {
        if(ArrayUtils.contains(forbiddenNumbers, number){
            int index = Arrays.binarySearch(array, number);
            while(index >= 0) {
                ArrayUtils.remove(array, index);
                index = Arrays.binarySearch(array, number);
            }
            ArrayUtils.remove(forbiddenNumbers, Arrays.binarySearch(forbiddenNumbers, number);
        } else {
            ArrayUtils.add(forbiddenNumbers, number);
        }
    }
}

, .

, , , , , , , .

, (, ), , .

+2

, O(n)

        int[] test = new int[] { 10, 13, 10, 10, 105};
        int tobeDel = 10;
        int[] tmp = new int[test.length];
        int j=0;
        for (int i=0;i<test.length;i++) {
            if (test[i] == tobeDel) {
                continue;
            }    
            tmp[j] = test[i];
            j++;
        }
        int[] result = new int[j];
        System.arraycopy(tmp, 0, result, 0, j);

, result.

+2

1

I never found a single function for your requirement, but this code will do.

while(ArrayUtils.removeElement(test,10).length!=test.length)
    test=ArrayUtils.removeElement(test,10);

You can use removeElement , which will remove the element and return a new array. If there is nothing to delete, then it will return the same list.


Decision 2

This will also do the same, but without any iteration. If this is what you are looking for.

Arrays.sort(test);
int[] finalValue =ArrayUtils.subarray(test, 0, ArrayUtils.indexOf(test,10));
ArrayUtils.addAll(finalValue,ArrayUtils.subarray(test,ArrayUtils.lastIndexOf(test,10), test.length));

This sorts arrays and cuts arrays from the beginning to the first appearance, and then from the last appearance to the end of the array. !!

+1
source

Source: https://habr.com/ru/post/1542890/


All Articles