Except for one, every element in an array of numbers appears twice.
Locate that solitary one.
Keep in mind that your algorithm should have a linear runtime complexity.
Could you do that without using any more memory?
class Solution {
public:
int singleNumber(vector<int>& nums) {
int result = 0;
for(auto & c : nums) {
result ^= c;
}
return result;
}
};
First of all, what sorts of keywords should I be paying attention to in order to figure out that I should be using an XOR operation for this question?
Also, why does XOR'ing all items in the vector with each other give us the one that is not repeated?