Java > Array-1 > firstLast6 (CodingBat Solution)

Problem:

Given an array of ints, return true if 6 appears as either the first or last element in the array. The array will be length 1 or more.

firstLast6({1, 2, 6}) → true
firstLast6({6, 1, 2, 3}) → true
firstLast6({13, 6, 1, 2, 3}) → false


Solution:

public boolean firstLast6(int[] nums) {
  if (nums[0] == 6 || nums[nums.length-1] == 6)
    return true;
  else return false;
}


3 comments :

  1. public boolean firstLast6(int[] nums) {
    return (nums.length == 0 || nums[0] == 6 || nums[nums.length - 1] == 6);
    }

    ReplyDelete
  2. public boolean firstLast6(int[] nums) {

    if(nums.length>=1&&(nums[0]==6||nums[nums.length-1]==6))
    return true;

    return false;
    }

    ReplyDelete
  3. public boolean firstLast6(int[] nums) {
    if (nums.length==0)return false;
    if (nums[0]==6) return true;
    else if (nums[nums.length-1]==6) 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