-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathFind Duplicate in Array
44 lines (24 loc) · 959 Bytes
/
Find Duplicate in Array
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
int Solution::repeatedNumber(const vector<int> &a) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
int sqrtn=sqrt(a.size()-1);
int store[sqrtn];
fill(store,store+sqrtn,0); int check=0;
for(int i=0;i<a.size();i++)
{
store[a[i]/sqrtn]++;
if(store[a[i]/sqrtn]>sqrtn)
check=a[i]/sqrtn;
}
map<int,int>mp;
for(int i=0;i<a.size();i++)
{
if(a[i]>=check && a[i]<=sqrtn+check)
mp[a[i]]++;
if(mp[a[i]]>1)
return a[i];
}
return -1;
}