-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathfirstRepeatingEle.cpp
More file actions
40 lines (34 loc) · 869 Bytes
/
Copy pathfirstRepeatingEle.cpp
File metadata and controls
40 lines (34 loc) · 869 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// 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.
class Solution {
public:
// Function to return the position of the first repeating element.
int firstRepeated(int arr[], int n) {
unordered_map<int,int> mpp;
for(int i=0;i<n;i++)
{
mpp[arr[i]]++;
}
for(int i=0;i<n;i++)
{
if(mpp[arr[i]] >= 2)
{
return i+1;
}
}
return -1;
}
};