Answers for "Arrays: move zeros to the left python"

1

Move Zeros to the End array

Array -- Move Zeros to the End
Write a method that can move all the zeros to  last indexes of 
the array (Do Not Use Sort Method)
Ex:
input:  {1,0,2,0,3,0,4,0};
output: [1, 2, 3, 4, 0, 0, 0, 0]
 
Solution:
public static int[] moveZeros( int[]  arr ) {
ArrayList<Integer> list = new ArrayList<>();
int countZero = 0;
for(int each: arr) {
list.add(each);
countZero+= (each==0)?1:0;
}

list.removeAll(Arrays.asList(0));
arr = new int[arr.length];
for(int i=0; i <arr.length-countZero; i++ ) {
arr[i] = list.get(i);
}
return arr;
}
Posted by: Guest on September-29-2021

Browse Popular Code Answers by Language