I think you did pretty good job on the algorithm here, as all array elements are checked and only nr-of-10 zeros are appended to the "good" numbers.
The one optimization I would add is to put the if
code outside the first loop, so it is not repeated for all array numbers but run only one time after first loop is done. The if
check itself is not needed anymore as condition is checked in for
loop. In code it will look like below:
public int[] withoutTen(int[] nums) {
int slow = 0;
// put "good" numbers in array head
for (int fast = 0; fast < nums.length; fast++) {
if (nums[fast] != 10) {
nums[slow] = nums[fast];
slow++;
}
}
// here we need to replace 10szero inout array tail
for (int i = slow; i < nums.length; i++) {
nums[i] = 0;
}
return nums;
}
Here is DEMO