-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathfirst_repeating_element.cpp
More file actions
63 lines (48 loc) · 875 Bytes
/
first_repeating_element.cpp
File metadata and controls
63 lines (48 loc) · 875 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Companies: Amazon, Oracle
First Repeating Element
Given an array of size N. Find the first repeating element in the array of integers.
Input:
7
1 5 3 4 3 5 6
Output:
1
*/
#include<iostream>
#include<climits>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++)
{
int num;
cin>>num;
arr[i] = num;
}
const int N = 1e6;
int idx[N];
for(int i=0; i<N; i++)
{
idx[i] = -1;
}
int minidx = INT_MAX;
for(int i=0; i<n; i++)
{
int val = arr[i];
if(idx[val]==-1)
{
idx[val] = i;
}
else{
if(idx[val]<minidx)
minidx = idx[val];
}
}
if(minidx==INT_MAX)
minidx = -1;
cout<<minidx<<endl;
return 0;
}