Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.

&nbsp;
Example 1:

Input: nums = [3,0,1]
Output: 2
Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.


Example 2:

Input: nums = [0,1]
Output: 2
Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.


Example 3:

Input: nums = [9,6,4,2,3,5,7,0,1]
Output: 8
Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.


&nbsp;
Constraints:


	n == nums.length
	1 &lt;= n &lt;= 104
	0 &lt;= nums[i] &lt;= n
	All the numbers of nums are unique.


&nbsp;
Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?

class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        # T.C = O(n) S.C = O(1)
        actualsum = 0
        currentsum = 0
        i = 1
        for num in nums:
            currentsum += num
            actualsum += i
            i += 1
        
        return actualsum - currentsum
// Approach 1: Find diff

class Solution {
    public int missingNumber(int[] nums) {
        int n = nums.length;
        int expectedSum = (n * (n + 1)) / 2;
        for (int num : nums)
            expectedSum -= num;
        return expectedSum;
    }
}

// Approach 2: XOR
class Solution {
    public int missingNumber(int[] nums) {
        int xor1 = 0;
        for (int i = 1; i <= nums.length; i++)
            xor1 = xor1 ^ i;

        int xor2 = 0;
        for (int num : nums)
            xor2 = xor2 ^ num;
        return xor1 ^ xor2;
    }
}

// Approach 3: Cyclic sort
class Solution {
    public int missingNumber(int[] nums) {
        
        int i = 0;
        while (i < nums.length) {

            if (nums[i] != i && nums[i] < nums.length)
                swap(i, nums[i], nums);
            else
                i += 1;
        }
        
        for (int j = 0; j < nums.length; j++) {
            if (nums[j] != j)
                return j;
        }
        return nums.length;
    }
    
    private void swap(int i, int j, int[] nums) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}
class Solution {
public:
    int missingNumber(vector<int>& nums) {
      int n=nums.size();
        long sum=n*(n+1)/2;
        long temp=0;
        for(int i=0;i<n;i++)
        {
            temp+=nums[i];

        }
        return sum-temp;
    }
};
var missingNumber = function(nums) {
    return ((1 + nums.length)*nums.length/2) - nums.reduce((a,b) => a+b)
};
Missing Number