# C++ solution O(n) time and O(1) space, easy-understaning with simple explanation

• ``````vector<int> singleNumber(vector<int>& nums) {
int aXorb = 0;  // the result of a xor b;
for (auto item : nums) aXorb ^= item;
int lastBit = (aXorb & (aXorb - 1)) ^ aXorb;  // the last bit that a diffs b
int intA = 0, intB = 0;
for (auto item : nums) {
// based on the last bit, group the items into groupA(include a) and groupB
if (item & lastBit) intA = intA ^ item;
else intB = intB ^ item;
}
return vector<int>{intA, intB};
}``````

• This is a very nice solution. Excellent!

• ``  lastBit = aXorb & (-aXorb)  //  my last bit :)``

• nice solution， clear thought

• Really smart solution !!!

• How smart you are!

• @lchen77 I think the standard way of finding the LSB (least significant bit) is `x & (-x)`, which I learned from binary indexed tree (fenwick tree).

• amazing solution!

• awesome man. really nice solution

• very nice solution!!!

• oh man very nice!

• @lchen77 Neat and beautiful!

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.