Java > Array-2 > withoutTen (CodingBat Solution)

Problem:

Return a version of the given array where all the 10's have been removed. The remaining elements should shift left towards the start of the array as needed, and the empty spaces a the end of the array should be 0. So {1, 10, 10, 2} yields {1, 2, 0, 0}. You may modify and return the given array or make a new array.

withoutTen({1, 10, 10, 2}) → {1, 2, 0, 0}
withoutTen({10, 2, 10}) → {2, 0, 0}
withoutTen({1, 99, 10}) → {1, 99, 0}


Solution:

public int[] withoutTen(int[] nums) {
  int[] result = new int[nums.length];
  int j = 0;
  for(int i = 0; i < nums.length; i++) {
    if(nums[i] == 10) {
    } else {
      result[j] = nums[i];
      j++;
    }
  }
  
  for(int i = j; i < nums.length; i++) {
    result[i] = 0;
  }
  return result;
}


16 comments :

  1. If you create new array, it will be filled zero's value by default. So, it is possible to solve this problem like this:
    public int[] withoutTen(int[] nums) {
    int[] tab = new int[nums.length];
    int counter = 0;
    for (int i = 0; i < nums.length; i++)
    {
    if (nums[i] != 10)
    tab[counter++] = nums[i];
    }
    return tab;
    }

    ReplyDelete
  2. without creating new array but 2 cycles:
    static int[] withoutTen(int[] nums) {
    int len = nums.length;

    for(int i=0; i<len; i++){
    if(nums[i]==10){
    for(int j=i; j<len-1; j++){
    nums[j] =nums[j+1];
    }
    nums[len-1]=0;
    i--;
    }
    }
    return nums;
    }

    ReplyDelete
  3. This is a different approach looks heavy but it isnt reallly
    public int[] withoutTen(int[] nums) {
    int ten = 0;
    for(int i = 0; i < nums.length; i++){
    if(nums[i] == 10)
    for(int j = i; j < nums.length; j++)
    if(nums[j] != 10){
    int temp = nums[i];
    nums[i] = nums[j];
    nums[j] = temp;
    break;
    }
    }
    for(int i = 0; i < nums.length; i++)
    if(nums[i] == 10)
    nums[i] = 0;

    return nums;
    }

    ReplyDelete
  4. public int[] withoutTen(int[] nums) {
    int count=0;
    int index=0;
    int i=0;
    int[] arr=new int[nums.length];
    while(i<nums.length){
    if(nums[i]!=10){
    arr[index]=nums[i];
    index++;
    i++;
    }
    else {count++; i++;}
    }
    for (int j = 0; j < count; j++) {
    arr[index]=0;
    index++;
    }
    return arr;
    }

    ReplyDelete
  5. Using only one loop---
    public int[] withoutTen(int[] nums) {
    int result = 0;
    for(int i = 0; i < nums.length; i++){
    if(nums[i] != 10){
    int temp = nums[i];
    nums[i] = 0;
    nums[result] = temp;
    result++;
    }else{
    nums[i] = 0;
    }
    }
    return nums;
    }

    ReplyDelete
  6. public int[] withoutTen(int[] nums) {
    int count10 = 0;
    int not10= 0;
    int[] arr = new int[nums.length];
    for(int i = 0; i < nums.length; i++){
    if(nums[i] != 10){
    arr[not10] = nums[i];
    not10++;
    }else{
    arr[nums.length - count10 - 1] = 0;
    count10++;
    }
    }
    return arr;
    }

    ReplyDelete
  7. public static int[] withoutTen(int[] nums) {
    int index = 0;
    for (int i = 0; i < nums.length; i++) {

    if(nums[i] != 10) {
    int temp = nums[i];
    nums[i] = nums[index];
    nums[index] = temp;
    index++;

    }
    if(nums[i] == 10) {
    nums[i] = 0;
    }

    }


    return nums;
    }

    ReplyDelete
  8. I liked my solution.

    int [] d= new int [nums.length];
    int count=0;
    for(int i=0;i<nums.length;i++) {

    if(nums[i]!=10) {

    d[count++]=nums[i];
    }
    }

    return d;

    ReplyDelete
  9. public int[] withoutTen(int[] nums) {
    int[] r = new int[nums.length];
    int x = 0;

    for(int i = 0 ; i < nums.length ; i++) {
    if(nums[i] != 10) {
    r[x] = nums[i];
    x++;
    }
    }

    return r;
    }

    ReplyDelete
  10. public int[] withoutTen(int[] nums) {
    int currentShift =0;
    int medium =0;

    for(int i=0; i<nums.length; i++){

    if(nums[i] ==10 || nums[i] ==0 ){
    currentShift ++;
    nums[i]=0;
    }
    if(nums[i]!=10 && currentShift !=0 && nums[i]!=0){
    medium = nums[i];
    nums[i-currentShift] = medium;
    nums[i]=0;
    currentShift =1;

    }

    }

    return nums;
    }

    ReplyDelete
  11. One for loop, without creating new array

    public int[] withoutTen(int[] nums) {

    for(int i = 0, j = 0; j < nums.length; j++) {
    if(nums[j] == 10) {
    nums[j] = 0;
    }
    else {
    if(nums[i] == 0) {
    nums[i] = nums[j];
    nums[j] = 0;
    }
    i++;
    }
    }
    return nums;

    }

    ReplyDelete
  12. public int[] withoutTen(int[] nums) {
    int a[]=new int[nums.length];
    int k=0;
    for(int i=0;i<nums.length;i++){
    if(nums[i]==10)
    continue;
    else
    a[k++]=nums[i];
    }
    return a;
    }

    ReplyDelete
  13. public int[] withoutTen(int[] nums) {
    //Two Pointer One Loop Solution//
    int i = 0;
    for (int j = 0; j < nums.length; j++)
    {
    if (nums[j] == 10)
    {
    while (i < nums.length && nums[i] == 10)
    {
    i++;
    }
    if (i < nums.length)
    {
    nums[j] = nums[i];
    nums[i] = 10;
    }
    else
    {
    nums[j] = 0;
    }
    }
    i++;
    }
    return nums;
    }

    ReplyDelete
  14. public int[] withoutTen(int[] nums) {
    int i = 0, j = 0;
    while (j < nums.length) {
    if (nums[j] != 10) {
    nums[i++] = nums[j];
    }
    j++;
    }
    while (i < nums.length) {
    nums[i++] = 0;
    }
    return nums;
    }

    ReplyDelete
  15. public class WithoutTen {
    public int[] withoutTen(int[] nums) {
    int[] tab = new int[nums.length];
    int counter = 0;
    for (int num : nums) {
    if (num != 10) {
    tab[counter++] = num;
    }
    }
    return tab;
    }
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact