Skip to content

Instantly share code, notes, and snippets.

@allucardster
Last active November 26, 2020 16:24
Show Gist options
  • Save allucardster/ab8dff6fb939d831629a0018c1380d1d to your computer and use it in GitHub Desktop.
Save allucardster/ab8dff6fb939d831629a0018c1380d1d to your computer and use it in GitHub Desktop.
Find the missing number in an array
function findMissingNumber(arr)
{
const n = arr.length + 1;
let num = ((n + 1) * n) / 2;
arr.forEach(val => {
num = num - val;
});
return num;
}
// Numbers from 1 to 100 withouth 51
let test = [...Array(100).keys()].map(x => x + 1).filter(x => x != 51);
findMissingNumber(test); //returns 51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment