Skip to content

Instantly share code, notes, and snippets.

@paulgrammer
Created August 23, 2021 18:57
Show Gist options
  • Save paulgrammer/05de5aa6b249f5f042aadc124b779719 to your computer and use it in GitHub Desktop.
Save paulgrammer/05de5aa6b249f5f042aadc124b779719 to your computer and use it in GitHub Desktop.
function findMissingValue(values) {
let seq = values.reduce((result, value, index) => {
let nextIndex = index + 1;
if (nextIndex < values.length) {
let deference = values[nextIndex] - value;
result.push(deference);
}
return result;
}, []);
let outcome = [];
let sequence = seq.filter((item, i, ar) => ar.indexOf(item) !== i)[0];
let maxCount = values[values.length - 1];
for (let i = values[0]; i <= maxCount; i = i + sequence) {
outcome.push(i);
}
return outcome.filter((i) => !values.includes(i));
}
let [missing] = findMissingValue([1, 3, 4, 5, 6]);
console.log(missing);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment