Created
October 26, 2020 10:11
-
-
Save nelsonfncosta/81a39b4a22f2ef30e72c79f5bc3e9e3d to your computer and use it in GitHub Desktop.
numDuplicates example
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 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