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

Given a sorted array of consecutive elements. The array has only one element repeated many times. The task is to find length of the sequence of repeated element.

Expected Time Complexity : Less than 0(n)

Examples:

Input : arr[] = {1, 2, 3, 4, 4, 4, 5, 6} Output : 4 3 Repeated element is 4 and it appears 3 times. Input : arr[] = {4, 4, 4, 4, 4} Output : 4 5 Input : arr[] = {6, 7, 8, 9, 10, 10, 11} Output : 10 2 Input : arr[] = {6, 7, 8, 9, 10, 10, 10} Output : 10 3

We need to find two things:

- Number of times the element repeats. If the array is sorted and if max-difference of two adjacent elements is 1, then the length of the repeated sequence is n – 1 – (array[n-1] – array[0])
- Value of the element. To the value, we do Binary Search.

## C++

`// C++ program to find the only repeated element ` `// and number of times it appears ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Assumptions : vector a is sorted, max-difference ` `// of two adjacent elements is 1 ` `pair<` `int` `, ` `int` `> sequence(` `const` `vector<` `int` `>& a) ` `{ ` ` ` `if` `(a.size() == 0) ` ` ` `return` `{0, 0}; ` ` ` ` ` `int` `s = 0; ` ` ` `int` `e = a.size() - 1; ` ` ` `while` `(s < e) ` ` ` `{ ` ` ` `int` `m = (s + e) / 2; ` ` ` ` ` `// if a[m] = m + a[0], there is no ` ` ` `// repeating character in [s..m] ` ` ` `if` `(a[m] >= m + a[0]) ` ` ` `s = m + 1; ` ` ` ` ` `// if a[m] < m + a[0], there is a ` ` ` `// repeating character in [s..m] ` ` ` `else` ` ` `e = m; ` ` ` `} ` ` ` `return` `{a[s], a.size() - (a[a.size() - 1] - a[0])}; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `pair<` `int` `, ` `int` `>p = sequence({ 1, 2, 3, 4, 4, 4, 5, 6 }); ` ` ` `cout << ` `"Repeated element is "` `<< p.first ` ` ` `<< ` `", it appears "` `<< p.second << ` `" times"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the only repeated element ` `// and number of times it appears ` ` ` `import` `java.awt.Point; ` `import` `java.util.Arrays; ` `import` `java.util.Vector; ` ` ` `class` `Test ` `{ ` ` ` `// Assumptions : vector a is sorted, max-difference ` ` ` `// of two adjacent elements is 1 ` ` ` `static` `Point sequence(Vector<Integer> a) ` ` ` `{ ` ` ` `if` `(a.size() == ` `0` `) ` ` ` `return` `new` `Point(` `0` `, ` `0` `); ` ` ` ` ` `int` `s = ` `0` `; ` ` ` `int` `e = a.size() - ` `1` `; ` ` ` `while` `(s < e) ` ` ` `{ ` ` ` `int` `m = (s + e) / ` `2` `; ` ` ` ` ` `// if a[m] = m + a[0], there is no ` ` ` `// repeating character in [s..m] ` ` ` `if` `(a.get(m) >= m + a.get(` `0` `)) ` ` ` `s = m + ` `1` `; ` ` ` ` ` `// if a[m] < m + a[0], there is a ` ` ` `// repeating character in [s..m] ` ` ` `else` ` ` `e = m; ` ` ` `} ` ` ` `return` `new` `Point(a.get(s), a.size() - (a.get(a.size() - ` `1` `) - a.get(` `0` `))); ` ` ` `} ` ` ` ` ` `// Driver method ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `Integer array[] = ` `new` `Integer[]{` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `4` `, ` `4` `, ` `5` `, ` `6` `}; ` ` ` `Point p = sequence(` `new` `Vector<>(Arrays.asList(array))); ` ` ` `System.out.println(` `"Repeated element is "` `+ p.x + ` ` ` `", it appears "` `+ p.y + ` `" times"` `); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the only repeated element ` `// and number of times it appears ` `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 ` `{ ` ` ` `// Assumptions : vector a is sorted, max-difference ` ` ` `// of two adjacent elements is 1 ` ` ` `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], there is no ` ` ` `// repeating character in [s..m] ` ` ` `if` `(a[m] >= m + a[0]) ` ` ` `s = m + 1; ` ` ` ` ` `// if a[m] < m + a[0], there is a ` ` ` `// repeating character in [s..m] ` ` ` `else` ` ` `e = m; ` ` ` `} ` ` ` `return` `new` `Point(a[s], a.Count - (a[a.Count - 1] - a[0])); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `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"` `); ` ` ` `} ` `} ` ` ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

Repeated element is 4, it appears 3 times

This article is contributed by **Rakesh Kumar**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Find missing element in a sorted array of consecutive numbers
- Replacing an element makes array elements consecutive
- Count smaller elements in sorted array in C++
- Count of smaller or equal elements in sorted array
- Count elements less than or equal to a given value in a sorted rotated array
- For each element in 1st array count elements less than or equal to it in 2nd array
- Largest element in the array that is repeated exactly k times
- Count elements smaller than or equal to x in a sorted matrix
- Last duplicate element in a sorted array
- k-th missing element in sorted array
- Maximum element in a sorted and rotated array
- Check for Majority Element in a sorted array
- Find first and last positions of an element in a sorted array
- Find the element that appears once in a sorted array
- Search an element in a sorted and rotated array