I am (still!) going through these CodingBat exercises for Java. Here is the one I have just done:
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.
And here is my code:
public int[] zeroFront(int[] nums){
int zeroCount = 0;
int[] resultantArray = new int[nums.length];
int[] noZerosArray = new int[nums.length];
//Count zeros
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
zeroCount++;
}
}
//Make an array without any zeros
int j = 0;
for (int i = 0; i < nums.length; i++) {
if(nums[i] != 0){
noZerosArray[j] = nums[i];
j++;
}
}
//To resultant array, add zeros first, then add remaining numbers of original
for (int i = 0; i < resultantArray.length; i++) {
if (i < zeroCount) {
resultantArray[i] = 0;
} else {
resultantArray[i] = noZerosArray[i-zeroCount];
}
}
return resultantArray;
}
Please bear in mind I am doing it without importing anything extra like java.util.Arrays
etc., as , primarily, this is not accepted by the assessor and, secondarily, I want to get to grips with arrays without importing anything extra yet.
Regarding my code, I would like to know how this can be improved. Is this a good solution? It feels like it could be more efficient, bearing in mind I have three for
loops and creating three extra arrays.
Even though this one seems trivial, I found it really difficult!