Count of only repeated element in a sorted array of consecutive elements | C # program to find the only repeated element and number of times it appears ; 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 code
using System ; using System . Collections . Generic ; public class Point { public int first , second ; public Point ( int first , int second ) { this . first = first ; this . second = second ; } } public class Test { static Point sequence ( List < int > a ) { if ( a . Count == 0 ) return new Point ( 0 , 0 ) ; int s = 0 ; int e = a . Count - 1 ; while ( s < e ) { int m = ( s + e ) / 2 ; if ( a [ m ] >= m + a [ 0 ] ) s = m + 1 ; else e = m ; } return new Point ( a [ s ] , a . Count - ( a [ a . Count - 1 ] - a [ 0 ] ) ) ; } public static void Main ( String [ ] args ) { int [ ] array = new int [ ] { 1 , 2 , 3 , 4 , 4 , 4 , 5 , 6 } ; Point p = sequence ( new List < int > ( array ) ) ; Console . WriteLine ( " Repeated ▁ element ▁ is ▁ " + p . first + " , ▁ it ▁ appears ▁ " + p . second + " ▁ times " ) ; } }