public class Check_Number {
public static boolean checkNumber(int i , int find, int []arr) {
if(i==arr.length) {
return false;
}
else {
if( arr[i] == find ) {
return true;
}
else {
return checkNumber(i+1,find,arr);
}
}
}
public static void main(String ... args) {
int [] arr = {1,2,3,4,5,6,7};
System.out.println(checkNumber(0,10,arr));
}
}
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (2)
Does Java have tail call optimisation now? If not, what are the advantages to this approach compared to Arrays.binarySearch(arr, find), Arrays.asList(arr).contains(find) or the various stream-related techniques?
No, Java does not support tail call optimization, and we cannot use Arrays.binarySearch(arr, find) as first we will have to sort the array first as binary search works only on sorted arrays. As far as Arrays.asList(arr) concern we first have to convert our array into Arraylist which would definately take time so solving it like this would be better than Arrays.asList(arr).contains(find). But if we have array list instead of fixed size array then we can go for that option.