• Status: Solved
• Priority: Medium
• Security: Public
• Views: 144

# firstLast6

Hi,
I was working on below challenge to find 6 at first and last position of array.

http://codingbat.com/prob/p185685

``````public boolean firstLast6(int[] nums) {

if(nums.contains(6))
{
return true;
}

else
{
return false;
}

}
``````

0
gudii9
• 2
• 2
2 Solutions

Author Commented:
``````public boolean firstLast6(int[] nums) {

int firstInt=nums[0];
int numsLength=nums.length;
int lastInt=nums[numsLength-1];

if(firstInt==6)
{
return true;
}

if(lastInt==6)
{
return true;
}

else
{
return false;
}

}
``````

i made changes and passed the tests. i wonder how i can improve my code. Please advise
0

Commented:
I don't see anything wrong with your code.
You can also do it without the temporary variables:
return nums[0]==6 || nums[nums.length-1]==6;
0

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

above passed all tests too in just one single simple line. couple  of questions here
1. What are temporary variables are there any permenant variables too if yes which to use when
2. How the false scenario taken care automatically how it passed all the tests( returning true is somewhat fine but returning false scenario not clear)

0

Commented:
there aren't really permanent variables, but some variables can be more temporary than others
http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

nums[0]==6 and nums[nums.length-1]==6 are either true or false.
0

Associate Software DeveloperCommented:
``````public boolean firstLast6(int[] nums) {
return nums[0] == 6 || nums[nums.length - 1] == 6;
}
``````
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Featured Post

• 2
• 2
Tackle projects and never again get stuck behind a technical roadblock.