Skip to content

Instantly share code, notes, and snippets.

@venelrene
Created May 20, 2020 13:52
Show Gist options
  • Save venelrene/58c21034ed53eb6543f423af0c1125ec to your computer and use it in GitHub Desktop.
Save venelrene/58c21034ed53eb6543f423af0c1125ec to your computer and use it in GitHub Desktop.
Your task is to find the first element of an array that is not consecutive. By not consecutive we mean not exactly 1 larger than the previous element of the array. E.g. If we have an array [1,2,3,4,6,7,8] then 1 then 2 then 3 then 4 are all consecutive but 6 is not, so that's the first non-consecutive number. If the whole array is consecutive th…
function firstNonConsecutive (arr) {
const min = Math.min(...arr);
const max = Math.max(...arr);
// add missing numbers in the array
let newArr = Array.from(Array(max-min), (v, i) => {
return i + min
});
// compare the full array with the old missing array
let filter = newArr.filter(i => {
return !arr.includes(i)
});
let nonConsecutivNum = filter[0]+1;
if (isNaN(nonConsecutivNum)) {
return null;
} else {
return nonConsecutivNum;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment