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 new int[] {}; else if (a.length !=0 && b.length == 0) return new int[]{a[0]}; else if (a.length == 0 && b.length != 0) return new int[]{b[0]}; else return new int[] {a[0],b[0]}; }
public int[] front11(int[] a, int[] b) {
ReplyDeleteint[] c=new int[a.length>0?b.length>0?2:1:b.length>0?1:0];
if(a.length>0){c[0]=a[0];}
if(b.length>0){c[a.length>0?1:0]=b[0];}
return c;
}
public int[] front11(int[] a, int[] b) {
Deleteif(a.length>=1&&b.length>=1)
return new int[] {a[0],b[0]};
if(a.length>1&&b.length<1)
return new int[] {a[0]};
if(a.length<1&&b.length>1)
return new int[] {b[0]};
return a;
}
public int[] front11(int[] a, int[] b) {
ReplyDeletereturn
a.length >= 1 && b.length >= 1 ? new int[] {a[0], b[0]} :
a.length >= 1 ? new int[] {a[0]} : b.length >= 1 ? new int[] {b[0]} : new int[0];
}
public int[] front11(int[] a, int[] b) {
ReplyDeleteif (b.length==0 && a.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]};
}
FASTEST AND EASIEST:
ReplyDeletepublic int[] front11(int[] a, int[] b) {
if(a.length >= 1 && b.length >= 1){
return new int[] {a[0], b[0]};
} else if(a.length == 0 && b.length >= 1) {
return new int[] {b[0]};
} else if (a.length >= 1 && b.length == 0){
return new int[] {a[0]};
} else {
return new int[] {};
}
}
public int[] front11(int[] a, int[] b) {
Deletereturn (a.length>0 && b.length == 0)?new int[] {a[0]}:
(a.length==0 && b.length>0)?new int[] {b[0]}:
(a.length>0 && b.length>0)?new int[] {a[0], b[0]}:
new int[] {};
}