Last active
November 26, 2020 17:31
-
-
Save allucardster/f3411193722d58e240a5dc90380cf684 to your computer and use it in GitHub Desktop.
Find missing numbers 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 getMissingNumbers(arr) { | |
arr.sort((a, b) => (a - b)); | |
const min = arr[0]; | |
const max = arr[arr.length - 1]; | |
const all = [...Array(max).keys()].map(x => x + min); | |
return all.filter(value => !arr.includes(value)); | |
} | |
// Numbers from 100 to 1 withouth 3 and 51 | |
const test = [...Array(100).keys()].map(x => x + 1).filter(x => (x != 51 && x != 3)).reverse() | |
getMissingNumbers(test); // returns [3, 51] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment