Front and Back Search in unsorted array | JavaScript program to implement front and back ; Start searching from both ends ; Keep searching while two indexes do not cross . ; Driver Code
function search ( arr , n , x ) { let front = 0 , back = n - 1 ; while ( front <= back ) { if ( arr [ front ] == x arr [ back ] == x ) return true ; front ++ ; back -- ; } return false ; } let arr = [ 10 , 20 , 80 , 30 , 60 , 50 , 110 , 100 , 130 , 170 ] ; let x = 130 ; let n = arr . length ; if ( search ( arr , n , x ) ) document . write ( " " ) ; else document . write ( " " ) ;