Skip to content

Instantly share code, notes, and snippets.

@CarlinCanales
Created September 15, 2021 14:43
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 CarlinCanales/cb5c05431a5dfc4003fe54c69935e5af to your computer and use it in GitHub Desktop.
Save CarlinCanales/cb5c05431a5dfc4003fe54c69935e5af to your computer and use it in GitHub Desktop.
Find distinct pairs that equal given number
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