First Repeating Element
Easy Accuracy: 48.62% Submissions: 16635 Points: 2

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:

N = 7
arr[] = {1, 5, 3, 4, 3, 5, 6}
Output: 2
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:

N = 4
arr[] = {1, 2, 3, 4}
Output: -1
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(NlogN)
Expected Auxilliary Space: O(N)


1 <= N <= 106
0 <= Ai<= 106

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

First Repeating Element

Output Window