Created
December 25, 2022 05:30
-
-
Save thmain/c52f17aa586372d230cfcdff88960bf8 to your computer and use it in GitHub Desktop.
This file contains 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 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