DEV Community

akbhairwal
akbhairwal

Posted on

6-10PM challenge problem #001 solution

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;
}

Top comments (1)

Collapse
 
itsraghz profile image
Raghavan alias Saravanan Muthu

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