Skip to content

Instantly share code, notes, and snippets.

@lienista
Last active August 3, 2020 11:45
Show Gist options
  • Select an option

  • Save lienista/9cdf83dbc5b6f2f7464d6e90324a7da3 to your computer and use it in GitHub Desktop.

Select an option

Save lienista/9cdf83dbc5b6f2f7464d6e90324a7da3 to your computer and use it in GitHub Desktop.
(Algorithms in Javascript) Leetcode 268. Missing Number - Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
const missingNumber = (nums) => {
let len = nums.length;
if(len===0) return 0;
result = 0;
for (let i=0; i<len; i++) {
result += nums[i] - i;
}
return len - result;
};
console.log(missingNumber([3,0,1]));
console.log(missingNumber([9,6,4,2,3,5,7,0,1]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment