Skip to main content
deleted 6 characters in body
Source Link
MaxZoom
  • 321
  • 3
  • 8

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

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 10s in array tail
  for (int i = slow; i < nums.length; i++) {
    nums[i] = 0;
  }
  return nums;
}

Here is DEMO

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 zero out array tail
  for (int i = slow; i < nums.length; i++) {
    nums[i] = 0;
  }
  return nums;
}

Here is DEMO

deleted 58 characters in body
Source Link
MaxZoom
  • 321
  • 3
  • 8

I think you did pretty good job on the algorithm here, as all the 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 will beis checked by forin 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 10s in array tail
  for (int i = slow; i < nums.length; i++) {
    nums[i] = 0;
  }
  return nums;
}

Here is DEMO

I think you did pretty good job on the algorithm here, as all the 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 is not needed anymore as condition will be checked by for loop. In code it will look like below:

public int[] withoutTen(int[] nums) {
  int slow = 0;

  for (int fast = 0; fast < nums.length; fast++) {
     if (nums[fast] != 10) {
       nums[slow] = nums[fast];
       slow++;
     }
  }
  
  // here we need to replace 10s
  for (int i = slow; i < nums.length; i++) {
    nums[i] = 0;
  }
  return nums;
}

Here is DEMO

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 10s in array tail
  for (int i = slow; i < nums.length; i++) {
    nums[i] = 0;
  }
  return nums;
}

Here is DEMO

deleted 58 characters in body
Source Link
MaxZoom
  • 321
  • 3
  • 8

I think you did pretty good job on the algorithm here, as all the 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 checkcode 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 is not needed anymore as condition will be checked by for loop. In code it will look like below:

public int[] withoutTen(int[] nums) {
  int slow = 0;

  for (int fast = 0; fast < nums.length; fast++) {
     if (nums[fast] != 10) {
       nums[slow] = nums[fast];
       slow++;
     }
  }
  
  if (slow != nums.length-1) {
    // only here we need to replace 10s
    for (int i = slow; i < nums.length; i++) {
      nums[i] = 0;
    }
  }
  return nums;
}

Here is DEMODEMO

I think you did pretty good job on the algorithm here, as all the 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 check outside the first loop, so it is not repeated for all array numbers but run only one time after first loop is done. In code it will look like below:

public int[] withoutTen(int[] nums) {
  int slow = 0;

  for (int fast = 0; fast < nums.length; fast++) {
     if (nums[fast] != 10) {
       nums[slow] = nums[fast];
       slow++;
     }
  }
  
  if (slow != nums.length-1) {
    // only here we need to replace 10s
    for (int i = slow; i < nums.length; i++) {
      nums[i] = 0;
    }
  }
  return nums;
}

Here is DEMO

I think you did pretty good job on the algorithm here, as all the 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 is not needed anymore as condition will be checked by for loop. In code it will look like below:

public int[] withoutTen(int[] nums) {
  int slow = 0;

  for (int fast = 0; fast < nums.length; fast++) {
     if (nums[fast] != 10) {
       nums[slow] = nums[fast];
       slow++;
     }
  }
  
  // here we need to replace 10s
  for (int i = slow; i < nums.length; i++) {
    nums[i] = 0;
  }
  return nums;
}

Here is DEMO

Source Link
MaxZoom
  • 321
  • 3
  • 8
Loading