You are viewing a single comment's thread. Return to all comments →
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; }
Seems like cookies are disabled on this browser, please enable them to open this website
Lonely Integer
You are viewing a single comment's thread. Return to all comments →
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; }