Java > Array-1 > commonEnd (CodingBat Solution)

Problem:

Given 2 arrays of ints, a and b, return true if they have the same first element or they have the same last element. Both arrays will be length 1 or more.

commonEnd({1, 2, 3}, {7, 3}) → true
commonEnd({1, 2, 3}, {7, 3, 2}) → false
commonEnd({1, 2, 3}, {1, 3}) → true


Solution:

public boolean commonEnd(int[] a, int[] b) {
  if (a[0] == b[0] || a[a.length-1] == b[b.length-1])
    return true;
  else
    return false;
}


2 comments :

  1. public boolean commonEnd(int[] a, int[] b) {
    return ((a[0] == b[0] || a[a.length-1] == b[b.length-1]));

    ReplyDelete
  2. public boolean commonEnd(int[] a, int[] b) {
    if (a.length==0 && b.length==0) return false;
    if (a[0]==b[0])return true;
    if(a[a.length-1]==b[b.length-1]) return true;
    else return false ;
    }

    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