We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Lonely Integer
- Discussions
Lonely Integer
Lonely Integer
Sort by
recency
|
242 Discussions
|
Please Login in order to post a comment
Shortest and Fastest solution is to use but manipulation XOR properties: a ^ a = 0 → XOR of a number with itself is 0
a ^ 0 = a → XOR of a number with 0 is the number
XOR is commutative and associative, so the order doesn't matter
public static int lonelyinteger(List a) { int result = 0; for (int num : a) { result ^= num; } return result; }
Swift:
Java using two hash sets which operations are O(1), therefore it is only needed to iterate whole array once.