Given an array **arr[]**, the task is to count the number of pairs formed by consecutive elements in which both of the elements in a pair are same.

**Examples:**

Input:arr[] = {1, 2, 2, 3, 4, 4, 5, 5, 5, 5}

Output:5

(1, 2), (4, 5), (6, 7), (7, 8) and (8, 9) are the valid index pairs

where consecutive elements are equal.

Input:arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Output:0

No two consecutive elements in the given array are equal.

**Approach:** Initialize **count = 0** and traverse the array from **arr[0]** to **arr[n – 2]**. If the current element is equal to the next element in the array then increment the **count**. Print the **count** in the end.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to return the count of consecutive ` `// elements in the array which are equal ` `int` `countCon(` `int` `ar[], ` `int` `n) ` `{ ` ` ` `int` `cnt = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) { ` ` ` ` ` `// If consecutive elements are same ` ` ` `if` `(ar[i] == ar[i + 1]) ` ` ` `cnt++; ` ` ` `} ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `ar[] = { 1, 2, 2, 3, 4, 4, 5, 5, 5, 5 }; ` ` ` `int` `n = ` `sizeof` `(ar) / ` `sizeof` `(ar[0]); ` ` ` `cout << countCon(ar, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `public` `class` `GfG ` `{ ` ` ` ` ` `// Function to return the count of consecutive ` ` ` `// elements in the array which are equal ` ` ` `static` `int` `countCon(` `int` `ar[], ` `int` `n) ` ` ` `{ ` ` ` `int` `cnt = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n - ` `1` `; i++) ` ` ` `{ ` ` ` ` ` `// If consecutive elements are same ` ` ` `if` `(ar[i] == ar[i + ` `1` `]) ` ` ` `cnt++; ` ` ` `} ` ` ` `return` `cnt; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `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)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Rituraj Jain ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count of consecutive ` `# elements in the array which are equal ` `def` `countCon(ar, n): ` ` ` `cnt ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(n ` `-` `1` `): ` ` ` ` ` `# If consecutive elements are same ` ` ` `if` `(ar[i] ` `=` `=` `ar[i ` `+` `1` `]): ` ` ` `cnt ` `+` `=` `1` ` ` ` ` `return` `cnt ` ` ` `# Driver code ` `ar ` `=` `[` `1` `, ` `2` `, ` `2` `, ` `3` `, ` `4` `, ` ` ` `4` `, ` `5` `, ` `5` `, ` `5` `, ` `5` `] ` `n ` `=` `len` `(ar) ` `print` `(countCon(ar, n)) ` ` ` `# This code is contributed by mohit kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GfG ` `{ ` ` ` ` ` `// Function to return the count of consecutive ` ` ` `// elements in the array which are equal ` ` ` `static` `int` `countCon(` `int` `[] ar, ` `int` `n) ` ` ` `{ ` ` ` `int` `cnt = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) ` ` ` `{ ` ` ` ` ` `// If consecutive elements are same ` ` ` `if` `(ar[i] == ar[i + 1]) ` ` ` `cnt++; ` ` ` `} ` ` ` `return` `cnt; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` ` ` `int` `[] ar = { 1, 2, 2, 3, 4, 4, 5, 5, 5, 5 }; ` ` ` `int` `n = ar.Length; ` ` ` `Console.WriteLine(countCon(ar, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Code_Mech. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the count of consecutive ` `// elements in the array which are equal ` `function` `countCon(` `$ar` `, ` `$n` `) ` `{ ` ` ` `$cnt` `= 0; ` ` ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `- 1; ` `$i` `++) ` ` ` `{ ` ` ` ` ` `// If consecutive elements are same ` ` ` `if` `(` `$ar` `[` `$i` `] == ` `$ar` `[` `$i` `+ 1]) ` ` ` `$cnt` `++; ` ` ` `} ` ` ` `return` `$cnt` `; ` `} ` ` ` `// Driver code ` `$ar` `= ` `array` `(1, 2, 2, 3, 4, 4, 5, 5, 5, 5); ` `$n` `= sizeof(` `$ar` `); ` `echo` `countCon(` `$ar` `, ` `$n` `); ` ` ` `// This code is contributed ` `// by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

5

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum number of elements to be removed so that pairwise consecutive elements are same
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- Count of all possible pairs of array elements with same parity
- Count of strings possible by replacing two consecutive same character with new character
- Count of elements which are second smallest among three consecutive elements
- Count array elements that can be represented as sum of at least two consecutive array elements
- Maximize product of same-indexed elements of same size subsequences
- Count of consecutive Fibonacci pairs in the given Array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Count distinct pairs from two arrays having same sum of digits
- Maximum count of pairs which generate the same sum
- Count pairs with average present in the same array
- Count of pairs in an Array with same number of set bits
- Delete consecutive same words in a sequence
- Minimum number of digits to be removed so that no two consecutive digits are same
- Length of the longest substring with no consecutive same letters
- Longest substring such that no three consecutive characters are same
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Minimize total cost without repeating same task in two consecutive iterations

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.