- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Count of only repeated element in a sorted array of consecutive elements in C++

We are given an array of consecutive numbers of length n. The array has only one number which is repeated more than once. The goal is to get the number of times that element is repeated in the array. Or we can say find the length of a repeated element in the array.

We will traverse the array from i=0 to i<n. If any arr[i]==arr[i+1] increment count. At last increment count by 1 for last element. Count will have length of repeated element.

Let’s understand with examples.

**Input** − arr[]= { 0,1,2,3,3,3 }, N=6

**Output** − Count of only repeated element − 3

**Explanation** − 3 is repeated thrice here.

**Input** − arr[]= { 1,2,3,4,4,4,4,4,5,6 }, N=10

**Output** − Count of only repeated element − 5

**Explanation** − 4 is repeated 5 times here.

## Approach used in the below program is as follows

We take an integer array arr[] initialized with consecutive numbers where one number is repeated.

Variable len stores the length of the array.

Function findRepeat(int arr[],int n) takes an array and its length as input and displays the repeated element value and length of repeated elements.

Take the initial count as 0.

Starting from index i=0 to i<n. If arr[i]==arr[i+1]. Increment count. Store element in variable value.

At the end of loop increment count by 1 for the last element.

Display element which is repeated as value.

Display number of repetitions as count.

## Example

#include <bits/stdc++.h> using namespace std; void findRepeat(int arr[],int n){ int count=0; //count of repeated element int value=0; //to store repeated element for(int i=0;i<n;i++){ if(arr[i]==arr[i+1]){ count++; value=arr[i]; } } count++; //for last element cout<<"Repeated Element: "<<value; cout<<endl<<"Number of occurrences: "<<count; } int main(){ int Arr[]={ 2,3,4,5,5,5,6,7,8 }; int len=sizeof(Arr)/sizeof(Arr[0]); findRepeat(Arr,len); return 0; }

## Output

If we run the above code it will generate the following output −

Repeated Element: 5 Number of occurrences: 3

- Related Articles
- Find missing element in a sorted array of consecutive numbers in C++
- Count smaller elements in sorted array in C++
- Find missing element in a sorted array of consecutive numbers in Python
- Count of smaller or equal elements in the sorted array in C++
- Find the only repeating element in a sorted array of size n using C++
- Find elements of array using XOR of consecutive elements in C++
- Single Element in a Sorted Array in C++
- Count number of occurrences (or frequency) in a sorted array in C++
- Print sorted distinct elements of array in C language
- Count of arrays having consecutive element with different values in C++
- Absolute distinct count in a sorted array in C++?
- C++ program to replace an element makes array elements consecutive
- Missing Element in Sorted Array in C++
- Find position of an element in a sorted array of infinite numbers in C++
- Count elements less than or equal to a given value in a sorted rotated array in C++