Skip to content

Instantly share code, notes, and snippets.

@allucardster
Last active November 26, 2020 17:31
Show Gist options
  • Save allucardster/f3411193722d58e240a5dc90380cf684 to your computer and use it in GitHub Desktop.
Save allucardster/f3411193722d58e240a5dc90380cf684 to your computer and use it in GitHub Desktop.
Find missing numbers in an array
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