3

I am curious what the most efficient method of removing null values in arrays is. Here is my current null(0) removal method.

public static int[] removeNull(int[] array){
        int j = 0;
        for( int i=0;  i<array.length;  i++ )
        {
            if (array[i] != 0)
                array[j++] = array[i];
        }
        int [] newArray = new int[j];
        System.arraycopy( array, 0, newArray, 0, j );
        return newArray;
}

What is this method performance? I was expecting it to be n.

1

2 Answers 2

5

Yes, your method's time complexity is O(n) - your loop has n (the length of the array) iterations, and copying an array requires time proportional to the size of the copied array, which in this case is O(n) in the worst case.

And you can't do better than that (in terms of time complexity), since you have to iterate over the entire array in order to locate the elements that should be removed.

If your goal is to reduce code complexity (i.e. write the shortest amount of code), you can use an IntStream (requires Java 8 or later):

public static int[] removeNull(int[] array) {
    return Arrays.stream(array).filter(i -> i != 0).toArray();
}

As Andreas commented, this solution has the advantage of leaving the original array unchanged.

Sign up to request clarification or add additional context in comments.

9 Comments

Entirely agree. +1 However, code complexity can be improved by replacing last 3 lines with return Arrays.copyOf(array, j);
@joshLor yes, though 2n is asymptotically O(n).
@Eran True, but I was sticking to plain non-stream Java, since so many on here are still pre-Java 8 Android users. --- One more +1 from me for adding stream solution. (Wish I could do another +1)
@joshLor 2n is the same thing as O(n) and 2n is also the same thing as O(n) - you got that right, twice :)
Suggested clarification: Stream solution is for Java 8+, and has the advantage that it leaves original array unmodified.
|
0
Use filter method,

import java.util.Arrays;
import java.util.stream.Collectors;

public class RemoveNullValue {
    public static void main( String args[] ) {
        String[] firstArray = {"test1", "", "test2", "test4", "", null};

        firstArray = Arrays.stream(firstArray)
                     .filter(s -> (s != null && s.length() > 0))
                     .toArray(String[]::new);    

    }
}

5 Comments

An int is not very probable to be null. In fact, I don't expect this to compile.
can check int with value of 0 and need to use Java 8
Still won't make code compile, since input is an int[], not an ArrayList<Integer>
can use Arrays.stream(arrayList).filter() for int[] from link String[] firstArray = {"test1", "", "test2", "test4", "", null}; firstArray = Arrays.stream(firstArray) .filter(s -> (s != null && s.length() > 0)) .toArray(String[]::new);
You should edit answer and fix it, rather than having (unformatted) code in a comment. Right now, answer is still wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.