# array6 challenfge

Hi,

I am working on below challenge.
http://codingbat.com/prob/p108997
Psedo code:
1. create new array of the portion of original array length
2. fill new array with rest of the elements
3. check to see if it has 6 if yes return true if no return false
4. retrun above boolean

I wrote my code as below
``````public boolean array6(int[] nums, int index) {
int len=nums.length-index;
int[] num=new int[len];
//fill without using for loop but recursive way
if(num.length==0){
return false;
}
if(num.length==1&&!num.contains(6)){
return false;
}
if(num.length==1&&num.contains(6)){
return true;
}

return null;
}``````
SOLUTION
rrz

membership
Create an account to see this answer
Signing up is free. No credit card required.

membership
Create an account to see this answer
Signing up is free. No credit card required.

java 8 has below lamda for each method but coding bat do not support

public boolean array6(int[] nums, int index) {
IntStream.of(nums).anyMatch(x -> x == 6);
}

``````public boolean array6(int[] nums, int index) {
if(index>nums.length()){
return false;
}
else if(nums[index]==6){
return true;
}
else return arrays6[nums,index+1];
}

/*- check if index >= array size
- if yes, return false
- check if nums[index] == 6
- if yes, return true,
- return array6 function call passing nums and index+1*/``````

above gives below error.

Compile problems:

Error:      else return arrays6[nums,index+1];
^
Syntax error on token ",", . expected

see Example Code to help with compile problems

``````public boolean array6(int[] nums, int index) {
if(index>=nums.length){
return false;
}
else if(nums[index]==6){
return true;
}
else return array6(nums,index+1);
}``````

above passed all tests.
any improvements or alternate approaches?
Your code is good.   My personal preference is to omit the elses.
``````public boolean array6(int[] nums, int index) {
if(index>=nums.length){
return false;
}
if(nums[index]==6){
return true;
}
return array6(nums,index+1);
}``````