1

This is a basic question but I do need some help.

Given two 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

I have the following code but it wont compile:

public boolean commonEnd(int[] a, int[] b) {
    if(a[0] == b[0] || a[a.length-1] ==b[b.length-1])
        return true;
}
7
  • Please always include compiler error messages when code does not compile. Commented Nov 16, 2012 at 4:53
  • "it is simple basic question but could u help me?" I hope in your next question you can put an upper-case letter for the start of each sentence, and spell words like 'you' properly. Till then.. Commented Nov 16, 2012 at 4:53
  • 1
    @AndrewThompson: Why not just...edit the question instead of nitpicking? Commented Nov 16, 2012 at 4:55
  • 1
    @Makoto I edit many. It is always a judgment call. This one did not make the cut. Commented Nov 16, 2012 at 4:56
  • 1
    If one of the answers got it to work for you, please mark that answer as accepted. (Click on the check mark to the left of the answer.) Commented Nov 16, 2012 at 5:03

5 Answers 5

4
  • You have missing right paren for the if.

  • You need to return something (false) in the "else" part.

You should have gotten a compiler error to tell you this.

Personally, I'd get rid of the if altogether and do

return  a[0] == b[0] || a[a.length-1] ==b[b.length-1];

(but this may be considered hard to read)

Sign up to request clarification or add additional context in comments.

1 Comment

I see nothing hard to read about this. Solid answer here. :)
0

you had no return type if, if is untrue

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

Comments

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

You have missed the return statement(if the if statement no true), that's why it didn't compile.

Comments

0

This code works fine:-

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

Comments

-1

Expects return Statements for the function

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

1 Comment

Actually the extra brace has occurred from workout for the probs sorry, updated the answer ...@SeanF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.