VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-repeating-element-sorted-array-size-n/

⇱ Find the only repeating element in a sorted array of size n - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find the only repeating element in a sorted array of size n

Last Updated : 19 Apr, 2024

Given a sorted array of n elements containing elements in range from 1 to n-1 i.e. one element occurs twice, the task is to find the repeating element in an array.

Examples :

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

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

Brute Force:

  1. Traverse the input array using a for a loop.
  2. For each element in the array, traverse the remaining part of the array using another for loop.
  3. For each subsequent element, check if it is equal to the current element.
  4. If a match is found, return the current element.
  5. If no match is found, continue with the next element in the outer loop.
  6. If the outer loop completes without finding a match, return -1 to indicate that there is no repeating element in the array.

Below is the implementation of the above approach:


Output
4

Time Complexity: O(N^2)
Auxiliary Space: O(1)

An efficient method is to use Floyd’s Tortoise and Hare algorithm.


Output
3

Time Complexity : O(log n)

Space Complexity: O(1)

Comment