Find repeating element in a sorted array of size n

Given a sorted an 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 an array using a for a loop.
  2. For each element in the array, traverse the remaining part of the an 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:

Python3

# Python3 program to find the just repeating element in an
# an array of size n and elements from range 1 to n-1.

# Returns second appearance of a repeating element
# The function/method assumes that an array elements are in range from
# 1 to n-1.
def FindRepeatingElement(arr, size):
    for i in range(size):
        for j in range (i+1, size):
            if arr[i] == arr[j]:
                return arr[i]
    return -1

# Driver code
if __name__ == '__main__':
    arr = [1, 2, 3, 4, 4]
    n = len(arr)
    element = FindRepeatingElement(arr, n)
    if element != -1:
        print(element)
Output

4

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

Python3

# Python program to find the just repeating element in an
# an array of size n and elements from range 1 to n-1

def findRepeatingElement(arr):
    tortoise = arr[0]
    hare = arr[0]
    
    # Phase 1: Finding intersection point of Tortoise and Hare
    while True:
        tortoise = arr[tortoise]
        hare = arr[arr[hare]]
        if tortoise == hare:
            break
    
    # Phase 2: Finding the entrance to the cycle
    tortoise = arr[0]
    while tortoise != hare:
        tortoise = arr[tortoise]
        hare = arr[hare]
    
    return hare

# Driver code
arr = [1, 2, 3, 3, 4, 5]
repeatingElement = findRepeatingElement(arr)
print(repeatingElement)
Output

3

Explore More

Python 10 Programs for Beginners

Program 1:Write a program to create a 2D array using NumPy. Output: Program 2: Write a program to convert a python list to a NumPy array. Output: Program 3: Write a program to create matrix of 3×3 from 11 to 30. Output: Program 4: Write a program to create a data frame to store data of candidates who appeared in

Find index of first occurrence when an unsorted array is sorted

Find index of first occurrence when an unsorted an array is sorted Given an unsorted an array and a number x, find an index of first occurrence of x when

Find sum non repeating distinct elements array

Find sum of non-repeating (distinct) elements in an array Given an integer an array with repeated elements, the task is to find the sum of all distinct elements in the