Missing Number

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

  • Time: O(n)
  • Space: O(1)
public int missingNumber(int[] nums) {
    int xor = 0, i;
    for (i = 0; i < nums.length; i++) {
        xor = xor ^ i ^ nums[i];
    }
    return xor ^ i;
}

results matching ""

    No results matching ""