Skip to content

Instantly share code, notes, and snippets.

@thmain
Created December 25, 2022 05:30
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 thmain/c52f17aa586372d230cfcdff88960bf8 to your computer and use it in GitHub Desktop.
Save thmain/c52f17aa586372d230cfcdff88960bf8 to your computer and use it in GitHub Desktop.
function twoDiff(arr, target) {
let map = {}
for (let i = 0; i < arr.length; i++) {
let sub = arr[i] - target
let add = arr[i] + target
if ( map[sub] || map[add] ) {
return true
} else {
map[arr[i]] = true
}
}
return false
}
console.log( twoDiff([1,2,3,4,5], 3) ) // true
console.log( twoDiff([5,4,3,2,1], 7) ) // false
console.log( twoDiff([5,4,3,2,1], 2) ) // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment