-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path136_singleNumber.cpp
More file actions
36 lines (28 loc) · 1.04 KB
/
136_singleNumber.cpp
File metadata and controls
36 lines (28 loc) · 1.04 KB
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
// 136. Single Number
class Solution {
public:
int singleNumber(vector<int>& nums) {
/*
// USING UNORDERED MAP FOR HASHMAP
unordered_map<int, int> hashmap;
for(int a:nums){
hashmap[a]+=1;
}
for(auto it=hashmap.begin();it!=hashmap.end();it++)
if(it->second==1) return it->first;
return -1;
*/
/*
GIVES THE FASTEST RUNTIME AND CONSTANT SPACE.
Bitwise XOR Operator(^) has three properties:
a^a = 0
0^a = a
XOR is associative just like addition, multiplication, etc which means that -> a^b^c = a^(b^c) = (a^b)^c = (a^c)^b
So, when we XOR all the numbers in the list, all the numbers that occur 2 times become 0 as a^a = 0. At the end, we will be left with an expression like 0^n, where n is the only number occurring once. Using second property 0^a = a :- 0^n = n. Hence, we get the unique number.
*/
int resultXOR = 0;
for(int a:nums)
resultXOR^=a;
return resultXOR;
}
};