forked from codedecks-in/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Single-Number-III.cpp
36 lines (32 loc) · 1.12 KB
/
Single-Number-III.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
// Problem URL: https://leetcode.com/problems/single-number-iii/
/*
* Runtime - 8ms
* Memory - 10mb
*/
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int xor_all = 0;
// Get the XOR value of all the numbers in the vector
for(int num: nums) xor_all ^= num;
// xor_all will contain a^b, where a and b are repeated only once.
// if value of a bit in xor is 1, then it means either a or b has
// 1 in that position, but not both. We can use this to find the answer.
int setbit = 1;
// Find the first position in xor_all where the value is 1
while((setbit & xor_all) == 0)
setbit <<= 1;
vector<int> result(2);
// We basically split the numbers into two sets.
// All numbers in first set will have a bit in the setbit position.
// Second set of numbers, will have 0 in the setbit position.
for(int num: nums) {
if(num & setbit) {
result[0] ^= num;
} else {
result[1] ^= num;
}
}
return result;
}
};