We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a50c105 commit 2952f11Copy full SHA for 2952f11
cpp/Bit Manipulation/lonely_integer.cpp
@@ -0,0 +1,13 @@
1
+#include <vector>
2
+
3
+int lonelyInteger(std::vector<int>& nums) {
4
+ int res = 0;
5
+ // XOR each element of the array so that duplicate values will
6
+ // cancel each other out (x ^ x == 0).
7
+ for (int i = 0; i < nums.size(); i++) {
8
+ res ^= nums[i];
9
+ }
10
+ // 'res' will store the lonely integer because it would not have
11
+ // been canceled out by any duplicate.
12
+ return res;
13
+}
0 commit comments