Longest subarray with all elements same | Function to find largest sub array with all equal elements . ; Traverse the array ; If element is same as previous increment temp value ; Return the required answer ; Driver code ; Function call
function subarray ( arr , n ) { var ans = 1 , temp = 1 ; for ( var i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) { ++ temp ; } else { ans = Math . max ( ans , temp ) ; temp = 1 ; } } ans = Math . max ( ans , temp ) ; return ans ; } var arr = [ 2 , 2 , 1 , 1 , 2 , 2 , 2 , 3 , 3 ] ; var n = arr . length ; document . write ( subarray ( arr , n ) ) ;