Maximum length of subarray such that all elements are equal in the subarray | Function to find the longest subarray with same element ; Check if the elements are same then we can increment the length ; Reinitialize j ; Compare the maximum length e with j ; Return max length ; Given array arr ; Function Call
function longest_subarray ( arr , d ) { var i = 0 , j = 1 , e = 0 ; for ( i = 0 ; i < d - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { j = j + 1 ; } else { j = 1 ; } if ( e < j ) { e = j ; } } return e ; } var arr = [ 1 , 2 , 3 , 4 ] ; var N = arr . length ; document . write ( longest_subarray ( arr , N ) ) ;