Skip to content
Related Articles

Related Articles

Find the first repeating element in an array of integers

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 19 Sep, 2022
View Discussion
Improve Article
Save Article

Given an array of integers arr[], The task is to find the index of first repeating element in it i.e. the element that occurs more than once and whose index of the first occurrence is the smallest. 

Examples: 

Input: arr[] = {10, 5, 3, 4, 3, 5, 6}
Output:
Explanation: 5 is the first element that repeats

Input: arr[] = {6, 10, 5, 4, 9, 120, 4, 6, 10}
Output:
Explanation: 6 is the first element that repeats

Recommended Practice

Naive Approach: Below is the idea to solve the problem

Run two nested loops, the outer loop picks an element one by one, and the inner loop checks whether the element is repeated or not. Once a repeating element is found, break the loops and print the element.

Time Complexity: O(N2)
Auxiliary Space: O(1)

Find the first repeating element in an array of integers using sorting:

Below is the idea to solve the problem.

Store the elements of arr[] in a duplicate array temp[], sort temp[] and traverse arr[] from 0 to N – 1, Simultaneously check the count of this element in temp[] and if the current element arr[i] has more than one occurrence then return arr[i].

Follow the steps below to Implement the idea: 

  • Copy the given array to an auxiliary array temp[] and sort temp array. 
  • Traverse the input array arr[] from 0 to N – 1
  • If no repeating element is found print “No Repeating Number Found”.

Time complexity: O(NlogN).
Auxiliary Space: O(N)

Find the first repeating element in an array of integers using Hashset

Below is the idea to solve the problem

The idea is to traverse the given array arr[] from right to left and update the minimum index whenever, an already visited element has been found. To check if the element was already visited Hashset can be used. 

Follow the steps below to implement the idea:

  • Initialize an empty Hashset myset and a variable min with -1.  
  • Run a for loop for each index of array arr[] from N – 1 to 0.
    • If the current element is present in myset then update min with i.
    • Else insert arr[i] in myset. 
  • Return min.

Below is the implementation of the above approach.

C++




/* C++ program to find first repeating element in arr[] */
#include <bits/stdc++.h>
using namespace std;
 
// This function prints the first repeating element in arr[]
void printFirstRepeating(int arr[], int n)
{
    // Initialize index of first repeating element
    int min = -1;
 
    // Creates an empty hashset
    set<int> myset;
 
    // Traverse the input array from right to left
    for (int i = n - 1; i >= 0; i--) {
        // If element is already in hash set, update min
        if (myset.find(arr[i]) != myset.end())
            min = i;
 
        else // Else add element to hash set
            myset.insert(arr[i]);
    }
 
    // Print the result
    if (min != -1)
        cout << "The first repeating element is "
             << arr[min];
    else
        cout << "There are no repeating elements";
}
 
// Driver Code
int main()
{
    int arr[] = { 10, 5, 3, 4, 3, 5, 6 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
    printFirstRepeating(arr, n);
}
// This article is contributed by Chhavi

Java




/* Java program to find first repeating element in arr[] */
import java.util.*;
 
class Main {
    // This function prints the first repeating element in
    // arr[]
    static void printFirstRepeating(int arr[])
    {
        // Initialize index of first repeating element
        int min = -1;
 
        // Creates an empty hashset
        HashSet<Integer> set = new HashSet<>();
 
        // Traverse the input array from right to left
        for (int i = arr.length - 1; i >= 0; i--) {
            // If element is already in hash set, update min
            if (set.contains(arr[i]))
                min = i;
 
            else // Else add element to hash set
                set.add(arr[i]);
        }
 
        // Print the result
        if (min != -1)
            System.out.println(
                "The first repeating element is "
                + arr[min]);
        else
            System.out.println(
                "There are no repeating elements");
    }
 
    // Driver method to test above method
    public static void main(String[] args)
        throws java.lang.Exception
    {
        int arr[] = { 10, 5, 3, 4, 3, 5, 6 };
        printFirstRepeating(arr);
    }
}

Python3




# Python3 program to find first repeating
# element in arr[]
 
# This function prints the first repeating
# element in arr[]
 
 
def printFirstRepeating(arr, n):
 
    # Initialize index of first repeating element
    Min = -1
 
    # Creates an empty hashset
    myset = dict()
 
    # Traverse the input array from right to left
    for i in range(n - 1, -1, -1):
 
        # If element is already in hash set,
        # update Min
        if arr[i] in myset.keys():
            Min = i
 
        else# Else add element to hash set
            myset[arr[i]] = 1
 
    # Print the result
    if (Min != -1):
        print("The first repeating element is",
              arr[Min])
    else:
        print("There are no repeating elements")
 
 
# Driver Code
arr = [10, 5, 3, 4, 3, 5, 6]
 
n = len(arr)
printFirstRepeating(arr, n)
 
# This code is contributed by Mohit kumar 29

C#




using System;
using System.Collections.Generic;
 
/* C# program to find first repeating element in arr[] */
 
public class GFG {
    // This function prints the first repeating element in
    // arr[]
    public static void printFirstRepeating(int[] arr)
    {
        // Initialize index of first repeating element
        int min = -1;
 
        // Creates an empty hashset
        HashSet<int> set = new HashSet<int>();
 
        // Traverse the input array from right to left
        for (int i = arr.Length - 1; i >= 0; i--) {
            // If element is already in hash set, update min
            if (set.Contains(arr[i])) {
                min = i;
            }
 
            else // Else add element to hash set
            {
                set.Add(arr[i]);
            }
        }
 
        // Print the result
        if (min != -1) {
            Console.WriteLine(
                "The first repeating element is "
                + arr[min]);
        }
        else {
            Console.WriteLine(
                "There are no repeating elements");
        }
    }
 
    // Driver method to test above method
 
    public static void Main(string[] args)
    {
        int[] arr = new int[] { 10, 5, 3, 4, 3, 5, 6 };
        printFirstRepeating(arr);
    }
}
 
// This code is contributed by Shrikant13

Javascript




<script>
 
// Javascript program to find first
// repeating element in arr[]
     
// This function prints the first
// repeating element in arr[]
function  printFirstRepeating(arr)
{
     
    // Initialize index of first
    // repeating element
    let min = -1;
 
    // Creates an empty hashset
    let set = new Set();
 
    // Traverse the input array from right to left
    for(let i = arr.length - 1; i >= 0; i--)
    {
         
        // If element is already in
        // hash set, update min
        if (set.has(arr[i]))
            min = i;
             
        // Else add element to hash set
        else 
            set.add(arr[i]);
    }
 
    // Print the result
    if (min != -1)
      document.write("The first repeating element is " +
                     arr[min]);
    else
      document.write("There are no repeating elements");
}
 
// Driver code
let arr = [ 10, 5, 3, 4, 3, 5, 6 ];
 
printFirstRepeating(arr);
 
// This code is contributed by unknown2108
     
</script>

Output

The first repeating element is 5

Time Complexity: O(n).
Auxiliary Space: O(n).

Thanks to Mohammad Shahid for suggesting this solution.

Find the first repeating element in an array of integers using Hashing 

The idea is to use Hash array to store the occurrence of elements. Then traverse the array from left to right and return the first element with occurrence more than 1.

Follow the below steps to implement the idea:

  • Initialize variables k with 0, max with -1 and min with max + 1 and iterate over all values of arr[] to store the largest value in max.
  • Initialize a Hash arrays a[] and b[] of size max + 1.
  • Run a for loop from 0 to N – 1
    • If a[arr[i]] is 0 put i+1 in place of a[arr[i]].
    • Else assign 1 to b[arrr[i]] and k.
  • If k is 0 print “No repeating element found”.
  • Else iterate from 0 to max 
    • If a[i] is not zero and b[i] is not zero and min is greater than a[i] then update min a[i].
  • Print min.

Below is the Implementation of above approach 

C++




/* C++ program to find first
repeating element in arr[] */
#include <bits/stdc++.h>
using namespace std;
 
// This function prints the
// first repeating element in arr[]
void printFirstRepeating(int arr[], int n)
{
 
    // This will set k=1, if any
    // repeating element found
    int k = 0;
 
    // max = maximum from (all elements & n)
    int max = n;
    for (int i = 0; i < n; i++)
        if (max < arr[i])
            max = arr[i];
 
    // Array a is for storing
    // 1st time occurrence of element
    // initialized by 0
    int a[max + 1] = {};
 
    // Store 1 in array b
    // if element is duplicate
    // initialized by 0
    int b[max + 1] = {};
 
    for (int i = 0; i < n; i++) {
 
        // Duplicate element found
        if (a[arr[i]]) {
            b[arr[i]] = 1;
            k = 1;
            continue;
        }
        else
            // storing 1st occurrence of arr[i]
            a[arr[i]] = i + 1;
    }
 
    if (k == 0)
        cout << "No repeating element found" << endl;
    else {
        int min = max + 1;
 
        // trace array a & find repeating element
        // with min index
        for (int i = 0; i < max + 1; i++)
            if (a[i] && min > a[i] && b[i])
                min = a[i];
        cout << arr[min - 1];
    }
    cout << endl;
}
 
// Driver method to test above method
int main()
{
    int arr[] = { 10, 5, 3, 4, 3, 5, 6 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
    printFirstRepeating(arr, N);
}

Java




/* Java program to find first
repeating element in arr[] */
public class GFG {
 
    // This function prints the
    // first repeating element in arr[]
    static void printFirstRepeating(int[] arr, int n)
    {
 
        // This will set k=1, if any
        // repeating element found
        int k = 0;
 
        // max = maximum from (all elements & n)
        int max = n;
        for (int i = 0; i < n; i++)
            if (max < arr[i])
                max = arr[i];
 
        // Array a is for storing
        // 1st time occurrence of element
        // initialized by 0
        int[] a = new int[max + 1];
 
        // Store 1 in array b
        // if element is duplicate
        // initialized by 0
        int[] b = new int[max + 1];
        for (int i = 0; i < n; i++) {
 
            // Duplicate element found
            if (a[arr[i]] != 0) {
                b[arr[i]] = 1;
                k = 1;
                continue;
            }
            else
                // storing 1st occurrence of arr[i]
                a[arr[i]] = i + 1;
        }
 
        if (k == 0)
            System.out.println(
                "No repeating element found");
        else {
            int min = max + 1;
 
            // trace array a & find repeating element
            // with min index
            for (int i = 0; i < max + 1; i++)
                if (a[i] != 0 && min > a[i] && b[i] != 0)
                    min = a[i];
            System.out.print(arr[min - 1]);
        }
        System.out.println();
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = { 10, 5, 3, 4, 3, 5, 6 };
 
        int N = arr.length;
        printFirstRepeating(arr, N);
    }
}
 
// This code is contributed by divyesh072019

Python3




# Python3 program to find first
# repeating element in arr[]
 
# This function prints the
# first repeating element in arr[]
 
 
def printFirstRepeating(arr, n):
 
    # This will set k=1, if any
    # repeating element found
    k = 0
 
    # max = maximum from (all elements & n)
    max = n
 
    for i in range(n):
        if (max < arr[i]):
            max = arr[i]
 
    # Array a is for storing
    # 1st time occurrence of element
    # initialized by 0
    a = [0 for i in range(max + 1)]
 
    # Store 1 in array b
    # if element is duplicate
    # initialized by 0
    b = [0 for i in range(max + 1)]
 
    for i in range(n):
 
        # Duplicate element found
        if (a[arr[i]]):
            b[arr[i]] = 1
            k = 1
            continue
        else:
 
            # Storing 1st occurrence of arr[i]
            a[arr[i]] = i+1
 
    if (k == 0):
        print("No repeating element found")
    else:
        min = max + 1
 
        for i in range(max + 1):
 
            # Trace array a & find repeating
            # element with min index
            if (a[i] and (min > (a[i])) and b[i]):
                min = a[i]
 
        print(arr[min-1])
 
 
# Driver code
arr = [10, 5, 3, 4, 3, 5, 6]
N = len(arr)
 
printFirstRepeating(arr, N)
 
# This code is contributed by avanitrachhadiya2155

C#




/* C# program to find first
repeating element in arr[] */
using System;
class GFG {
 
    // This function prints the
    // first repeating element in arr[]
    static void printFirstRepeating(int[] arr, int n)
    {
 
        // This will set k=1, if any
        // repeating element found
        int k = 0;
 
        // max = maximum from (all elements & n)
        int max = n;
        for (int i = 0; i < n; i++)
            if (max < arr[i])
                max = arr[i];
 
        // Array a is for storing
        // 1st time occurrence of element
        // initialized by 0
        int[] a = new int[max + 1];
 
        // Store 1 in array b
        // if element is duplicate
        // initialized by 0
        int[] b = new int[max + 1];
 
        for (int i = 0; i < n; i++) {
 
            // Duplicate element found
            if (a[arr[i]] != 0) {
                b[arr[i]] = 1;
                k = 1;
                continue;
            }
            else
                // storing 1st occurrence of arr[i]
                a[arr[i]] = i + 1;
        }
 
        if (k == 0)
            Console.WriteLine("No repeating element found");
        else {
            int min = max + 1;
 
            // trace array a & find repeating element
            // with min index
            for (int i = 0; i < max + 1; i++)
                if ((a[i] != 0) && min > a[i]
                    && (b[i] != 0))
                    min = a[i];
            Console.Write(arr[min - 1]);
        }
        Console.WriteLine();
    }
 
    // Driver code
    static void Main()
    {
        int[] arr = { 10, 5, 3, 4, 3, 5, 6 };
 
        int N = arr.Length;
        printFirstRepeating(arr, N);
    }
}
 
// This code is contributed by divyeshrabadiya07.

Javascript




<script>
/* javascript program to find first
repeating element in arr */
  
 
    // This function prints the
    // first repeating element in arr
    function printFirstRepeating(arr , n) {
 
        // This will set k=1, if any
        // repeating element found
        var k = 0;
 
        // max = maximum from (all elements & n)
        var max = n;
        for (i = 0; i < n; i++)
            if (max < arr[i])
                max = arr[i];
 
        // Array a is for storing
        // 1st time occurrence of element
        // initialized by 0
        var a = Array(max + 1).fill(0);
 
        // Store 1 in array b
        // if element is duplicate
        // initialized by 0
        var b = Array(max + 1).fill(0);
        for (var i = 0; i < n; i++) {
 
            // Duplicate element found
            if (a[arr[i]] != 0) {
                b[arr[i]] = 1;
                k = 1;
                continue;
            } else
                // storing 1st occurrence of arr[i]
                a[arr[i]] = i+1;
        }
 
        if (k == 0)
            document.write("No repeating element found");
        else {
            var min = max + 1;
 
            // trace array a & find repeating element
            // with min index
            for (i = 0; i < max + 1; i++)
                if (a[i] != 0 && min > a[i] && b[i] != 0)
                    min = a[i];
            document.write(arr[min-1]);
        }
        document.write("<br/>");
    }
 
    // Driver code
     
        var arr = [ 10, 5, 3, 4, 3, 5, 6 ];
 
        var N = arr.length;
        printFirstRepeating(arr, N);
 
// This code is contributed by todaysgaurav
</script>

Output

5

Time Complexity: O(N).
Auxiliary Space: O(N).


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!