-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathDuplicateinarray.cpp
66 lines (45 loc) · 1.15 KB
/
Duplicateinarray.cpp
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
64
65
66
/*
Name: Mehul Chaturvedi
IIT-Guwahati
*/
/*
PROBLEM STATEMENT
Given an array of integers of size n which contains numbers from 0 to n - 2. Each number is present at least once. That is, if n = 5, numbers from 0 to 3 is present in the given array at least once and one number is present twice. You need to find and return that duplicate number present in the array.
Assume, duplicate number is always present in the array.
Input format :
Line 1 : Size of input array
Line 2 : Array elements (separated by space)
Output Format :
Duplicate element
*/
#include <bits/stdc++.h>
using namespace std;
int MissingNumber(int arr[], int size){
unordered_map<int, int> m1;
for (int i = 0; i < size; ++i)
{
m1[arr[i]]++;
}
for (int i = 0; i < size; ++i)
{
if (m1[arr[i]] == 2)
{
return arr[i];
}
}
return -1;
}
int main( int argc , char ** argv )
{
ios_base::sync_with_stdio(false) ;
cin.tie(NULL) ;
int size;
cin >> size;
int *input = new int[1 + size];
for(int i = 0; i < size; i++)
cin >> input[i];
cout << MissingNumber(input, size);
delete [] input;
return 0;
return 0 ;
}