Last active
November 26, 2020 16:24
-
-
Save allucardster/ab8dff6fb939d831629a0018c1380d1d to your computer and use it in GitHub Desktop.
Find the missing number in an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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