Count consecutive pairs of same elements | Java implementation of the approach ; Function to return the count of consecutive elements in the array which are equal ; If consecutive elements are same ; Driver Code
public class GfG { static int countCon ( int ar [ ] , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( ar [ i ] == ar [ i + 1 ] ) cnt ++ ; } return cnt ; } public static void main ( String [ ] args ) { int ar [ ] = { 1 , 2 , 2 , 3 , 4 , 4 , 5 , 5 , 5 , 5 } ; int n = ar . length ; System . out . println ( countCon ( ar , n ) ) ; } }