-
Notifications
You must be signed in to change notification settings - Fork 160
/
Find the duplicate element.cpp
71 lines (43 loc) · 1.1 KB
/
Find the duplicate element.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
67
68
69
70
/*
Given a list of integers of length n in the range of 1 to n-1, find the duplicate element (only one duplicate).
For example:
Input: {2,3,1,5,4,6,3}
Output: 3
*/
/*
solution1: use a for loop to compute the sum of the list, then returns sum-(n-1)*n/2. Sum may overflow.
O(n) time, O(1) space
*/
/*
solution2: take the array as a hashtable. The array will be modified.
O(n) time, O(1) space
*/
int FindDuplication(int A[], int n) {
for (int i = 0; i < n; ++i) {
while (A[i] != i+1) {
if ( A[ A[i]-1 ] == A[i]) {
return A[i];
}
swap(A[i], A[ A[i]-1 ]); //swap the element to correct position
}
}
}
/*
solution3: sort the array first, check consecutive elements. The array will be modified.
O(nlogn) time, O(1) space
*/
/*
solution4: use XOR.
O(n) time, O(1) space.
*/
int Duplicate(int A[], int n) {
int XOR1 = 1;
for (int i = 2; i <= n-1; ++i) {
XOR1 = XOR1 ^ i;
}
int XOR2 = A[0];
for(int i = 1;i < n; ++i) {
XOR2 = XOR2 ^ A[i];
}
return XOR1 ^ XOR2;
}