First Repeating Element | Practice | GeeksforGeeks

Given an array arr[] of size n, find the first repeating element. The element should occurs more than once and the index of its first occurrence should be the smallest.
 
Example 1:
Input:
n = 7
arr[] = {1, 5, 3, 4, 3, 5, 6}
Output: 2
Explanation: 
5 is appearing twice and 
its first appearence is at index 2 
which is less than 3 whose first 
occuring index is 3.

Example 2:
Input:
n = 4
arr[] = {1, 2, 3, 4}
Output: -1
Explanation: 
All elements appear only once so 
answer is -1.

Your Task:
You don't need to read input or print anything. Complete the function firstRepeated() which takes arr and n as input parameters and return the position of the first repeating element. If there is no such element, return -1.
The position you return should be according to 1-based indexing. 
 
Expected Time Complexity: O(n)
Expected Auxilliary Space: O(n)
 
Constraints:
1 <= n <= 106
0 <= Ai<= 106
0
sayanghoshk613 days ago
c++
int firstRepeated(int arr[], int n) {       unordered_map<int,int>mp;       for(int i=0;i<n;i++){           mp[arr[i]]++;       }       for(int i=0;i<n;i++){           if(mp[arr[i]]>1)           return i+1;        }       return -1;// code here   }
0
shubham211019976 days ago
 public static int firstRepeated(int[] arr, int n) {
      HashMap<Integer,Integer>h=new HashMap<>();
     
     for(int i:arr){
         h.put(i,h.getOrDefault(i,0)+1);
     }
   for(int i=0;i<n;i++){
       if(h.get(arr[i])>1) return i+1;
   }
   return -1;
   }
0
shubham211019976 days ago
it is not index but need to be position
0
iamnobodyji1 week ago
Java easy solution:-
HashMap<Integer,Integer> map = new HashMap<>();
        for(int h: arr){
            map.putIfAbsent(h,0);
            map.put(h,map.get(h)+1);
        }
        for(int i=0;i<n;i++){
            if(map.get(arr[i])>=2) return i+1;
        }
        return -1;
0
pradeepshillare1 week ago
class Solution {   // Function to return the position of the first repeating element.   public static int firstRepeated(int[] arr, int n)    {       // Your code here              HashMap<Integer,Integer> hs = new HashMap<Integer,Integer>();       int ans = -1;              for(int i=0;i<arr.length;i++)       {           if(hs.containsKey(arr[i])==false)           {               hs.put(arr[i],1);           }           else           {               int s = hs.get(arr[i]);               hs.put(arr[i],s+1);           }       }       int i=0;       while(i<arr.length) {           if (hs.get(arr[i]) > 1) {               ans = i+1;               break;           } else               i++;       }          return ans;   }} 
0
vishutyagi71 week ago
c++(easy) O(n) and O(n)
int firstRepeated(int arr[], int n)    {   int i,min=5000;   unordered_map<int, int> m1;   for(i=0;i<n;i++)   {       auto it=m1.find(arr[i]);       if(it!=m1.end())       {           if(m1[arr[i]]<min)           {               min=m1[arr[i]];           }       }       else       {           m1[arr[i]]=i;       }   }   if(min!=5000)   {   return min+1;   }   else   {       return -1;   }
 
0
kousikbarik20001 week ago
def repeatEle(nums):    num_set = set()    no_duplicate = -1    for i in range(len(nums)):        if nums[i] in num_set:            return nums[i]        else:            num_set.add(nums[i])    return no_duplicateprint(repeatEle([1, 2, 3, 4, 4, 5]))print(repeatEle([1,2,3,4]))print(repeatEle([1, 1, 2, 3, 3, 2, 2]))
+2
moryasivam2 weeks ago
int firstRepeated(int arr[], int n) {      unordered_map<int,int>m;           for(int i=0;i<n;i++){          m[arr[i]]++;      }         for(int i=0;i<n;i++){          if(m[arr[i]]>1){             return i+1;          }      }      return -1;   }
+1
mohitraj27412 weeks ago
0.26/2.43
O(n) time and space;
   
int firstRepeated(int arr[], int n) {              int min_ind=INT_MAX;       int x=*max_element(arr,arr+n)+1;              vector<int> visited(x, -1);       for(int i=0;i<=n-1;i++)       {           if(visited[arr[i]]==-1)           visited[arr[i]]=i;           else           min_ind=min(min_ind,visited[arr[i]]+1);       }       return (min_ind<INT_MAX) ? min_ind :-1;   }
0
vcreateforme92 weeks ago
class Solution { public:   // Function to return the position of the first repeating element.   int firstRepeated(int arr[], int n) {       // code here       map<int,int> m;       int MIN=-2;       while((--n)>-1)       {           m[arr[n]]++;           if(m[arr[n]]>=2){           MIN=n;           }       }       return MIN+1;   }};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.