```
var missingNumber = function(nums) {
// This assumes mergesort O(n) mem and O(n log n) speed.
// If quicksort is used then O(n^2) worst case speed O(n log n) average speed. O(log n) memory average O(n) worst case.
nums.sort(function (a, b){
return a-b;
});
var left = 0,
right = nums.length - 1,
mid = 0,
val;
while(left <= right){
mid = Math.floor((left + right) / 2);
val = nums[mid];
if(mid === val){
left = mid + 1;
}else{
right = mid - 1;
}
}
return (left > right) ? left : right;
};
```