Created
September 15, 2021 14:43
-
-
Save CarlinCanales/cb5c05431a5dfc4003fe54c69935e5af to your computer and use it in GitHub Desktop.
Find distinct pairs that equal given number
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 stockPairs(stocksProfit, target) { | |
// Write your code here | |
const pairs = []; | |
stocksProfit.forEach((profit1, idx1) => { | |
stocksProfit.forEach((profit2, idx2) => { | |
if (idx1 === idx2) return; | |
if (profit1 + profit2 === target) { | |
pairs.push([profit1, profit2]); | |
} | |
}); | |
}); | |
const totalPairs = pairs.map(pair => { | |
return [...pair].sort().join('') | |
}); | |
const distinctPairs = new Set(totalPairs); | |
console.log(distinctPairs); | |
return distinctPairs.size; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment