Sansa and XOR

  • + 0 comments

    Java, O(n)

    public static int sansaXor(List<Integer> arr) {
            int n = arr.size();
            if (n % 2 == 0) {
                return 0; 
            } else {
                int result = 0;
                for (int i = 0; i < n; i += 2) {
                    result ^= arr.get(i); 
                }
                return result;
            }
        }