Count of only repeated element in a sorted array of consecutive elements | Assumptions : vector a is sorted , max - difference of two adjacent elements is 1 ; if a [ m ] = m + a [ 0 ] , there is no repeating character in [ s . . m ] ; if a [ m ] < m + a [ 0 ] , there is a repeating character in [ s . . m ] ; Driver method
function sequence ( a ) { if ( a . length == 0 ) return [ 0 , 0 ] let s = 0 let e = a . length - 1 while ( s < e ) { let m = Math . floor ( ( s + e ) / 2 ) ; if ( a [ m ] >= m + a [ 0 ] ) s = m + 1 else e = m } return [ a [ s ] , a . length - ( a [ a . length - 1 ] - a [ 0 ] ) ] } let p = sequence ( [ 1 , 2 , 3 , 4 , 4 , 4 , 5 , 6 ] ) document . write ( " " + p [ 0 ] + " " + p [ 1 ] + " " )