Java Array Program · A17
Move All Zeros to the End of an Array in Java
Copy every non-zero value forward to the next free slot, then fill the rest with zeros. The non-zero values keep their relative order.
Problem Statement
Rearrange an int array in place so that all zeros are at the end and all other values keep their original relative order.
Do not create a second array.
Input/Output Examples
[0, 6, 0, 2, 9]Output[6, 2, 9, 0, 0]Explanation: 6, 2 and 9 keep their order.[0, 0, 1]Output[1, 0, 0]Explanation: one value moves to the front.[4, 5]Output[4, 5]Explanation: nothing changes.Visual Explanation
- Copy forwardarray62929write = 3 after the scan
- Fill zerosarray62900from write to the end
- Done[6, 2, 9, 0, 0]
[0, 6, 0, 2, 9]: non-zero values are copied to positions 0, 1, 2 (purple); positions 3 and 4 still hold old values and are overwritten with zeros (amber).The write index never passes the read index, so no value is overwritten before it has been read. That is what makes the in-place copy safe.
Approaches
Both approaches use two indexes in one array. The first copies then fills; the second swaps as it goes.
Approach 1: Write Index, Then Fill (Recommended)
Idea: copy each non-zero value to arr[write++], then set the remaining positions to zero.
- Scan the array; for each non-zero value, write it at
writeand increment. - After the scan, set every position from
writeto the end to 0.
Time: O(n) · Space: O(1).
Approach 2: Swap in One Pass
Idea: keep the index of the first zero; swap each non-zero value with it.
- Set
firstZero = 0. - For each non-zero
arr[i], swap it witharr[firstZero]and incrementfirstZero. - Zeros drift to the end as values are swapped forward.
Time: O(n) · Space: O(1).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Write Index, Then Fill (Recommended)
Two simple loops and the fewest comparisons.
import java.util.Arrays;
public class ZerosWriteIndex {
// Copy every non-zero value forward, then fill the rest with zeros
static void moveZeros(int[] arr) {
int write = 0;
for (int v : arr) {
if (v != 0) {
arr[write++] = v;
}
}
while (write < arr.length) {
arr[write++] = 0;
}
}
public static void main(String[] args) {
int[][] inputs = {{0, 6, 0, 2, 9}, {0, 0, 1}, {4, 5}};
for (int[] arr : inputs) {
String before = Arrays.toString(arr);
moveZeros(arr);
System.out.println(before + " -> " + Arrays.toString(arr));
}
}
}
[0, 6, 0, 2, 9] -> [6, 2, 9, 0, 0] [0, 0, 1] -> [1, 0, 0] [4, 5] -> [4, 5]
Time complexity: O(n) · Space complexity: O(1).
Approach 2: Swap in One Pass
A single pass, and fewer writes when the array has few zeros near the front.
import java.util.Arrays;
public class ZerosSwap {
// Swap each non-zero value with the first zero seen so far, in a single pass
static void moveZeros(int[] arr) {
int firstZero = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != 0) {
int t = arr[firstZero];
arr[firstZero] = arr[i];
arr[i] = t;
firstZero++;
}
}
}
public static void main(String[] args) {
int[][] inputs = {{0, 6, 0, 2, 9}, {0, 0, 1}, {4, 5}};
for (int[] arr : inputs) {
String before = Arrays.toString(arr);
moveZeros(arr);
System.out.println(before + " -> " + Arrays.toString(arr));
}
}
}
[0, 6, 0, 2, 9] -> [6, 2, 9, 0, 0] [0, 0, 1] -> [1, 0, 0] [4, 5] -> [4, 5]
Time complexity: O(n) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Write Index, Then Fill | O(n) | O(1) |
| Swap in One Pass | O(n) | O(1) |
Here n is the number of elements. Both approaches read each element once and use two indexes.
Interview Tips and Recommended Approach
- Keep the order. Swapping zeros with the last element is faster to write but breaks the order of non-zero values.
- Explain why it is safe. The write index is never ahead of the read index.
- Generalise it. The same pattern removes all occurrences of any value in place.
- Count writes if asked. The swap version avoids rewriting values that are already in place.
Recommended Approach: Write Index, Then Fill
Why: it keeps the non-zero values in order, works in place and is the simplest two-index pattern to explain.