DEV Community

loading...

6-10PM challenge problem #001 solution

akbhairwal profile image akbhairwal ・1 min read

Problem#001

Move Zeros To End

public int[] moveZerosToEnd(int[] arr) {
    int j = 0;
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] != 0) {
            arr[j] = arr[i];
            j++;
        }
    }
    for (; j < arr.length; j++) {
        arr[j] = 0;
    }

    return arr;
}

Discussion

pic
Editor guide
Collapse
itsraghz profile image
Raghavan alias Saravanan Muthu

A good problem and a nice solution - matching with my thoughts. Thank you!