Solved

# firstLast6

Posted on 2015-02-12
92 Views
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
Question by:gudii9
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 7

Author Comment

ID: 40606490
``````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

LVL 84

Accepted Solution

ozo earned 400 total points
ID: 40606501
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

LVL 7

Author Comment

ID: 40606508
``````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

LVL 84

Expert Comment

ID: 40606515
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

LVL 3

Assisted Solution

Nadia Sobnom earned 100 total points
ID: 40606640
``````public boolean firstLast6(int[] nums) {
return nums[0] == 6 || nums[nums.length - 1] == 6;
}
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
###### Suggested Courses
Course of the Month5 days, 18 hours left to enroll