Skip to content

Instantly share code, notes, and snippets.

@AbeEstrada
Created September 3, 2025 03:26
Show Gist options
  • Save AbeEstrada/9e22122c8ba54fa5240c4e7caa6d7d01 to your computer and use it in GitHub Desktop.
Save AbeEstrada/9e22122c8ba54fa5240c4e7caa6d7d01 to your computer and use it in GitHub Desktop.
Exercise: Hash Tables Ice Cream Parlor
function whatFlavors(cost, targetAmount) {
const priceToIndexMap = new Map();
for (const [currentIndex, currentPrice] of cost.entries()) {
const neededPrice = targetAmount - currentPrice;
const displayIndex = currentIndex + 1;
if (priceToIndexMap.has(neededPrice)) {
const firstFlavorIndex = priceToIndexMap.get(neededPrice);
console.log(`${firstFlavorIndex} ${displayIndex}`);
return;
}
priceToIndexMap.set(currentPrice, displayIndex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment