Open In App

Find the first repeating element in an array of integers

Last Updated : 18 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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

Naive Approach – O(n^2) Time and O(1) Space

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.

C++
// Cpp to find first repeating element
// using Naive approach in O(n^2) Time and O(1) Space
#include <bits/stdc++.h>
using namespace std;

// Function to find the index of the first
// repeating element in a vector
int firstRepeatingElement(vector<int> &arr)
{
    int n = arr.size();

    // Nested loop to check for repeating elements
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (arr[i] == arr[j]) {
                return i;
            }
        }
    }

    // If no repeating element is found, return -1
    return -1;
}

int main()
{
    vector<int> arr = {10, 5, 3, 4, 3, 5, 6};
    int index = firstRepeatingElement(arr);
    if (index == -1)
        cout << "No repeating found!" << endl;
    else
        cout << "First repeating is " << arr[index] << endl;
    return 0;
}
Java Python C# JavaScript

Output
First repeating is 5

Expected Approach – Hash Set – O(n) Time and O(n) Space

Follow the steps below to implement the idea:

  • Initialize an empty Hash Set s
  • Run a for loop for each index i
    • If the current element is present in the hash set, then return i
    • Else insert arr[i] in s. 
  • Return -1 if no repeating element found

Below is the implementation of the above approach.

C++
// Cpp program to find first repeating element
// using Hash Set in O(n) Time and O(n) Space
#include <iostream>
#include <vector>
#include <unordered_set>
#include <climits>
using namespace std;

int firstRepeatingElement(const vector<int>& arr) {
    unordered_set<int> s;

    // If an element is already present, return it
    // else insert it
      int minEle = INT_MAX;
    for (int i = arr.size() - 1; i >= 0; i--) {
        if (s.find(arr[i]) != s.end()) {
            minEle = min(minEle, i);
        }
        s.insert(arr[i]);
    }
  
    // If no element repeats
    return minEle == INT_MAX? -1: minEle;
}

int main()
{
    vector<int> arr = {10, 5, 3, 4, 3, 5, 6};
    int index = firstRepeatingElement(arr);
    if (index == -1)
        cout << "No repeating found!" << endl;
    else
        cout << "First repeating is " << arr[index] << endl;
    return 0;
}
Java Python C# JavaScript

Output
First repeating is 3




Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg