Lonely Integer

  • + 0 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; }