Skip to content

Instantly share code, notes, and snippets.

@niradler
Created August 11, 2018 18:35
Show Gist options
  • Save niradler/b8926fd15bce1ac5e98e1efde163d047 to your computer and use it in GitHub Desktop.
Save niradler/b8926fd15bce1ac5e98e1efde163d047 to your computer and use it in GitHub Desktop.
find the missing number 1- 1000
// find the missing number 1- 1000
const min =0, max=1000;
let arr = new Array(max).fill(0).map((v,k)=> k);
const erase = Math.floor(Math.random() * (max - min + 1)) + min;
arr.splice(erase,1);
const findMissing = (arr) => {
const expected_sum = new Array(max).fill(0).map((v,k)=> k).reduce((sum,i) => sum + i);
const sum = arr.reduce((sum,i) => sum + i);
return expected_sum - sum;
}
const res = findMissing(arr);
console.log(res == erase,res, erase);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment