Skip to content

Instantly share code, notes, and snippets.

@nelsonfncosta
Created October 26, 2020 10:11
Show Gist options
  • Save nelsonfncosta/81a39b4a22f2ef30e72c79f5bc3e9e3d to your computer and use it in GitHub Desktop.
Save nelsonfncosta/81a39b4a22f2ef30e72c79f5bc3e9e3d to your computer and use it in GitHub Desktop.
numDuplicates example
function logItem() {
console.log(Array.from(arguments).join("\t"))
}
function numDuplicates(name, price, weight) {
const uniqueItems = new Set()
name.forEach(
(item, index) => {
const itemPrice = price[index]
const itemWeight = weight[index]
logItem(item, itemPrice, itemWeight)
uniqueItems.add(`${item}_${itemPrice}_${itemWeight}`)
}
)
return name.length - uniqueItems.size
}
/*
// 1
const name = ['ball', 'bat', 'glove', 'glove', 'glove']
const price = [2, 3, 1, 2, 1]
const weight = [2, 5, 1, 1, 1]
*/
// 2
const name = ['ball', 'box', 'ball', 'ball', 'box']
const price = [2, 2, 2, 2, 2]
const weight = [1, 2, 1, 1, 3]
const count = numDuplicates(name, price, weight)
console.log(`# duplicated items: ${count}`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment