Last active
July 28, 2022 02:22
-
-
Save MVAodhan/a873dbb877bce26079bf6994daf12d6d 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
let listA = [1, 4, 5, 6]; | |
let listB = [2, 3, 4, 5, 6]; | |
const biggerArray = (listA, listB) => { | |
let greater; | |
let lesser; | |
if (listA.length > listB.length) { | |
greater = listA | |
lesser = listB; | |
}else { | |
greater = listB; | |
lesser = listA; | |
} | |
return {greater, lesser} | |
}; | |
const findIntersection = (listA, listB) => { | |
let {greater, lesser}= biggerArray(listA,listB) | |
let greaterSet = new Set(greater) | |
let lesserSet = new Set(lesser); | |
let result | |
for(let num of greaterSet) { | |
let numPlus = num + 1; | |
let numMinus = num - 1; | |
if(lesserSet.has(numMinus)){ | |
result = [numMinus, num] | |
return result; | |
} | |
if (lesserSet.has(numPlus)) { | |
result = [num, numPlus]; | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment