Return an array that contains the exact same numbers as the given array, but rearranged so that all the zeros are grouped at the start of the array. The order of the non-zero numbers does not matter. So {1, 0, 0, 1} becomes {0 ,0, 1, 1}. You may modify and return the given array or make a new array.
zeroFront({1, 0, 0, 1}) → {0, 0, 1, 1}
zeroFront({0, 1, 1, 0, 1}) → {0, 0, 1, 1, 1}
zeroFront({1, 0}) → {0, 1}
Here is what I have, but I know that it is incredibly wrong.
public int[] zeroFront(int[] nums) {
for (int index = 0; index < nums.length; index ++)
{
int [] array1 = new int [index];
if (nums [index] == 0)
{
array1 = nums [index];
}
}
for (int index = 0; index < nums.length; index ++)
{
int [] array2 = new int [nums.length-index];
if (nums [index] != 0)
{
array2 = nums [index];
}
}
return array1 + array2;
}