Back to DSA
Missing Number
mediumAn array holds n distinct integers drawn from the range [0, n]. Exactly one number in that range is absent. Find and return it.
Examples
Example 1:
Input:
nums = [0,1,3]Output:
2 Example 2:
Input:
nums = [5,3,0,1,4,2,7,6]Output:
8Hints
class Solution {
public int missingNumber(int[] nums) {
int n = nums.length, result = n;
for (int i = 0; i < n; i++) result ^= i ^ nums[i];
return result;
}
}Time complexity
O(n)Space complexity
O(1)