Skip to content

Instantly share code, notes, and snippets.

@ahafidi
Created December 28, 2021 17:28
Show Gist options
  • Save ahafidi/ca21e9ab6fe9b60dae9c1bcbd5b601c7 to your computer and use it in GitHub Desktop.
Save ahafidi/ca21e9ab6fe9b60dae9c1bcbd5b601c7 to your computer and use it in GitHub Desktop.
const findSmallestInterval = (numbers) => {
numbers.sort((a, b) => a - b)
let r = Number.MAX_SAFE_INTEGER
numbers.forEach((e, i) => {
if (i + 1 > numbers.length)
return
const d = Math.abs(e - numbers[i + 1])
if (d < r)
r = d
})
return r
}
// [1, 6, 4, 2, 8] => 1 (between 2 and 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment