Skip to content

Instantly share code, notes, and snippets.

@robertbenjamin
Created December 13, 2016 21:49
Show Gist options
  • Save robertbenjamin/0837699b53f54e794bbcecb752e56992 to your computer and use it in GitHub Desktop.
Save robertbenjamin/0837699b53f54e794bbcecb752e56992 to your computer and use it in GitHub Desktop.
import java.util.*;
public class removeZeros {
public static void main(String[] args) {
int[] list = {7, 2, 3, 0, 4, 6, 0, 0, 13, 0, 78, 0, 0, 19, 14};
removeZeroes(list);
System.out.println(Arrays.toString(list));
}
public static void removeZeroes(int[] list) {
for (int i = 0; i < list.length - 1; i++) {
if (list[i] == 0) {
int num = i;
while (list[num] == 0 && num < list.length - 1) {
num++;
}
list[i] = list[num];
list[num] = 0;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment