-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector.cpp
More file actions
37 lines (32 loc) · 812 Bytes
/
Copy pathvector.cpp
File metadata and controls
37 lines (32 loc) · 812 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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
// finding the unique element
// of 2n+1 elements
int main() {
int size;
cin >> size;
vector<int> v(size);
for (int i = 0; i < size; i++) {
cin >> v[i];
}
// method 1
// sort(v.begin(), v.end());
// for (int i = 0; i < size; i += 2) {
// if (v[i] != v[i+1]) {
// cout << v[i];
// break;
// }
// }
// method 2
// Here we are taking XOR of all the elements
// A property that a^a = 0 will make all pairs 0
// and 0^b = b will give out the unpaired element
int res = 0;
for (int i = 0; i < size; i++) {
res = res ^ v[i];
}
cout << res << endl;
// method 2 is better as we dont need to sort the array
}