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)
A good problem and a nice solution - matching with my thoughts. Thank you!