

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the only repeating element in a sorted array of size n using C++
In this problem, we are given an arr[] of size N containing values from 1 to N-1 with one value occuring twice in the array. Our task is to find the only repeating element in a sorted array of size n.
Let’s take an example to understand the problem,
Input
arr[] = {1, 2, 3, 4, 5, 5, 6, 7}
Output
5
Solution Approach
A simple approach to solve the problem is by using linear search and checking if arr[i] and arr[i+1] have the same value. In this case, return arr[i] which is the value repeated.
Example 1
Program to illustrate the working of our solution
#include <iostream> using namespace std; int findRepeatingValueArr(int arr[], int N){ for(int i = 0; i < N; i++){ if(arr[i] == arr[i+1]) return (arr[i]); } return -1; } int main(){ int arr[] = {1, 2, 3, 4, 4, 5, 6}; int N = sizeof(arr)/sizeof(arr[0]); cout<<"The repeating value in the array is "<<findRepeatingValueArr(arr, N); return 0; }
Output
The repeating value in the array is 4
Another approach to solve the problem is by using a binary search algorithm to find the element that occurred twice at mid index. If the middle index value repeats itself then print it. If it is not at index position, traverse right subarray otherwise traverse left subarray.
Example 2
Program to illustrate the working of our solution
#include <bits/stdc++.h> using namespace std; int findRepeatingValueArr(int arr[], int s, int e){ if (s > e) return -1; int mid = (s + e) / 2; if (arr[mid] != mid + 1){ if (mid > 0 && arr[mid]==arr[mid-1]) return arr[mid]; return arr[findRepeatingValueArr(arr, s, mid-1)]; } return arr[findRepeatingValueArr(arr, mid+1, e)]; } int main(){ int arr[] = {1, 2, 3, 4, 5, 6, 6, 7, 8, 9}; int n = sizeof(arr) / sizeof(arr[0]); cout<<"The repeating value in the array is "<<findRepeatingValueArr(arr, 0, n-1);; return 0; }
Output
The repeating value in the array is 6
- Related Questions & Answers
- Find the only missing number in a sorted array using C++
- Find the first repeating element in an array of integers C++
- Find a sorted subsequence of size 3 in linear time in Python\n
- Find the only different element in an array using C++
- Count of only repeated element in a sorted array of consecutive elements in C++
- Find any one of the multiple repeating elements in read only array in C++
- Find the only repetitive element between 1 to n-1 using C++
- Find missing element in a sorted array of consecutive numbers in C++
- Find missing element in a sorted array of consecutive numbers in Python
- Find the element that appears once in sorted array - JavaScript
- Search in a Sorted Array of Unknown Size in C++
- Find position of an element in a sorted array of infinite numbers in C++
- Find First and Last Position of Element in Sorted Array in Python
- Find element in a sorted array whose frequency is greater than or equal to n/2 in C++.
- Repeating only even numbers inside an array in JavaScript