-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_number2.cpp
34 lines (31 loc) · 987 Bytes
/
single_number2.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
#include <unordered_map>
#include <iostream>
using namespace std;
class Solution {
public:
int singleNumber(int A[], int n) {
unordered_map<int, int> hash(n);
unordered_map<int, int>::iterator j;
typedef pair<int, int> pii;
for (int i = 0; i < n; i++) {
j = hash.find(A[i]);
if (j == hash.end()) {
pii t(A[i], 1);
hash.insert(t);
} else if (j->second == 2){
hash.erase(j);
} else {
j->second++;
}
}
return hash.begin()->first;
}
};
int main()
{
//int a[] = {1,1,1,2,2,2,3,3,3,4,5,5,5};
int a[] = {2};
Solution s;
cout << s.singleNumber(a, sizeof(a)/sizeof(a[0])) << endl;
return 0;
}