LeetCode – Missing Number (Java)

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example, given nums = [0, 1, 3] return 2.

Java Solution 1 - Math

public int missingNumber(int[] nums) {
    int sum=0;
    for(int i=0; i<nums.length; i++){
        sum+=nums[i];
    }
 
    int n=nums.length;
    return n*(n+1)/2-sum;
}

Java Solution 2 - Bit

public int missingNumber(int[] nums) {
 
    int miss=0;
    for(int i=0; i<nums.length; i++){
        miss ^= (i+1) ^nums[i];
    }
 
    return miss;
}

Java Solution 3 - Binary Search

public int missingNumber(int[] nums) {
   Arrays.sort(nums);
   int l=0, r=nums.length;
   while(l<r){
       int m = (l+r)/2;
       if(nums[m]>m){
           r=m;
       }else{
           l=m+1;
       }
   }
 
   return r;
}
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>