Created
August 23, 2021 18:57
-
-
Save paulgrammer/05de5aa6b249f5f042aadc124b779719 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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