Skip to content

Instantly share code, notes, and snippets.

@bcherny
Created October 7, 2019 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcherny/bcf52c9a24f11d16bb9fa4b380458966 to your computer and use it in GitHub Desktop.
Save bcherny/bcf52c9a24f11d16bb9fa4b380458966 to your computer and use it in GitHub Desktop.
function longestSeq(arr, of) {
let lengths = {}
let max = 0
arr.forEach(n => {
if (lengths[n - of]) {
lengths[n] = lengths[n - of] + 1
delete lengths[n - of]
} else {
lengths[n] = 1
}
if (lengths[n] > max) {
max = lengths[n]
}
})
return max
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment