Lonely Integer

  • + 0 comments

    This problem is most optimally solved using bit manipulation. The XOR logical function returns 1 when both inputs are diferent and 0 when they're the same, so 3 ^ 3 = 0 because in 11 xor 11 all digits are the same and they cancel out, this way we'll end up with the following: 0 ^ {lonelyInteger} which is the lonelyInteger itself: Java code:

        public static int lonelyinteger(List<Integer> a) {
        // Write your code here
            int res = 0;
            for(Integer i: a) {
                res ^= i;
            }
            return res;
        }