Java > Array-1 > front11 (CodingBat Solution)

Problem:

Given 2 int arrays, a and b, of any length, return a new array with the first element of each array. If either array is length 0, ignore that array.

front11({1, 2, 3}, {7, 9, 8}) → {1, 7}
front11({1}, {2}) → {1, 2}
front11({1, 7}, {}) → {1}


Solution:

public int[] front11(int[] a, int[] b) {
  if (a.length == 0 && b.length == 0) {
    return a;
  }
  if (a.length == 0) {
    int[] myArray = new int[] {b[0]};
    return myArray;
  }
  
  if (b.length == 0) {
    int[] myArray = new int[] {a[0]};
    return myArray;
  }
  
  int[] myArray = new int[] {a[0], b[0]};
  return myArray;    
}


7 comments :

  1. public int[] front11(int[] a, int[] b) {
    int []c =new int[0];

    if(a.length>0 && b.length>0){
    c = new int[2];
    c[0]=a[0];
    c[1]=b[0];
    }else if( a.length == 0 && b.length>1){
    c = new int[1];
    c[0]=b[0];
    }else if(b.length == 0 && a.length>1){
    c = new int[1];
    c[0]=a[0];
    }
    return c;

    }

    ReplyDelete
  2. public int[] front11(int[] a, int[] b) {
    if(a.length == 0 && b.length == 0){
    return new int[]{};
    }
    if(a.length == 0){
    return new int[]{b[0]};
    }
    if(b.length == 0){
    return new int[]{a[0]};
    }
    return new int[]{a[0], b[0]};
    }

    ReplyDelete
  3. public int[] front11(int[] a, int[] b) {
    int[] arr=new int[2];
    int[] ar1=new int[1];
    int[] ar2=new int[0];
    int aLen=a.length;
    int bLen=b.length;
    if(aLen==0 && bLen==1){
    arr[0]=b[0];
    return arr;
    }
    if((aLen==1 && bLen==0) || (aLen>1 && bLen==0)){
    ar1[0]=a[0];
    return ar1;
    }
    if((aLen==0 && bLen==0)){
    return ar2;
    }
    if((aLen==0 && bLen==1) || (aLen==0 && bLen>0)){
    ar1[0]=b[0];
    return ar1;
    }
    if((aLen>2 && bLen>0)||(aLen==1 && bLen==1)||(aLen==1 && bLen>1)){
    arr[0]=a[0];
    arr[1]=b[0];
    return arr;
    } return a;
    }

    ReplyDelete
  4. public int[] front11(int[] a, int[] b) {
    if(a.length>=1 && b.length>=1)
    return new int[] {a[0],b[0]};
    else if (a.length>=1 && !(b.length>=1))
    return new int [] {a[0]};
    else if (!(a.length>=1) && b.length>=1)
    return new int [] {b[0]};
    else return a;
    }

    ReplyDelete
  5. public int[] front11(int[] a, int[] b) {
    if(a.length!=0&&b.length!=0) return new int[] {a[0],b[0]};
    if(a.length==0&&b.length==0)return new int[] {};
    if(a.length==0) return new int[] { b[0]};
    else return new int[] {a[0]};
    }

    ReplyDelete
  6. public int[] front11(int[] a, int[] b) {
    if(a.length==0 && b.length==0)
    return a;
    else if(a.length!=0 && b.length!=0)
    {
    int t[]={a[0],b[0]};
    return t;
    }
    else if(a.length==0)
    {
    int t[]={b[0]};
    return t;
    }

    int t[]={a[0]};
    return t;
    }

    ReplyDelete
  7. public int[] front11(int[] a, int[] b) {
    return ((a.length>0)&&(b.length>0))? new int [] {a[0],b[0]} :
    (a.length>0)? new int [] {a[0]} : (b.length>0)? new int [] {b[0]}:
    new int []{};
    }

    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