使用哈希检查 3 个数组之间是否存在重复产品
Check if there is a duplicate product between 3 arrays using hash
我有一个产品列表,其数据已拆分为 3 个数组,其中包含产品的名称、价格和重量。如何创建使用哈希映射查找重复产品的函数?
//Inputs:
name = ["ball", "bat", "glove", "glove", "glove"]
price = [2, 3, 1, 2, 1]
weight = [2, 5, 1, 1, 1]
//Output: true
//Inputs:
name = ["ball", "bat", "glove", "glove", "glove"]
price = [2, 3, 1, 2, 2]
weight = [2, 5, 1, 1, 2]
//Output: false
我会首先质疑将数据拆分为三个数组的需要,然后如果无法重塑数据结构,则最终以这种方式解决问题。
const findDupes = (
[name, ...names],
[price, ...prices],
[weight, ...weights],
res = {},
) => {
const next = ($res) => findDupes(names, prices, weights, $res);
const hash = `${name}\/${price}\/${weight}`;
const dupe = res[hash] ? { name, price, weight } : [];
return [].concat(dupe).concat(
names.length ? next({ ...res, [hash]: true }) : [],
);
};
const name = ["ball", "bat", "glove", "glove", "glove"];
const price = [2, 3, 1, 2, 1];
const weight = [2, 5, 1, 1, 1];
console.log(
findDupes(name, price, weight),
);
Javascript 没有 HashSet
,但有一个 Set
可以存储任何你喜欢的东西。因此,要测试重复项:
创建一个空的Set
遍历 i = 0..items.length
对于每个 i
,从 name[i]
、price[i]
和 weight[i]
创建一个散列。注意:你怎么做并不重要,只要满足以下条件(注意 3x ===
):
hash(name[i], price[i], weight[i]) === hash(name[i], price[i], weight[i])
创建哈希后,检查它是否已经存在于您的 Set
- 如果存在,则存在重复,您可以立即return
- 如果没有,则将散列添加到集合中并继续循环
不提前到达循环末尾return意味着没有重复
let name, price, weight;
const makeHash = (name, price, weight) =>
`${name}__${price}__${weight}`;
const duplicateTest = () => {
const seen = new Set();
for (let i = 0; i < name.length; i += 1) {
const hash = makeHash(name[i], price[i], weight[i]);
if (seen.has(hash)) return true;
seen.add(hash);
}
return false;
}
name = ["ball", "bat", "glove", "glove", "glove"]
price = [2, 3, 1, 2, 1]
weight = [2, 5, 1, 1, 1]
console.log(duplicateTest()); // Output: true
name = ["ball", "bat", "glove", "glove", "glove"]
price = [2, 3, 1, 2, 2]
weight = [2, 5, 1, 1, 2]
console.log(duplicateTest()); // Output: false
我有一个产品列表,其数据已拆分为 3 个数组,其中包含产品的名称、价格和重量。如何创建使用哈希映射查找重复产品的函数?
//Inputs:
name = ["ball", "bat", "glove", "glove", "glove"]
price = [2, 3, 1, 2, 1]
weight = [2, 5, 1, 1, 1]
//Output: true
//Inputs:
name = ["ball", "bat", "glove", "glove", "glove"]
price = [2, 3, 1, 2, 2]
weight = [2, 5, 1, 1, 2]
//Output: false
我会首先质疑将数据拆分为三个数组的需要,然后如果无法重塑数据结构,则最终以这种方式解决问题。
const findDupes = (
[name, ...names],
[price, ...prices],
[weight, ...weights],
res = {},
) => {
const next = ($res) => findDupes(names, prices, weights, $res);
const hash = `${name}\/${price}\/${weight}`;
const dupe = res[hash] ? { name, price, weight } : [];
return [].concat(dupe).concat(
names.length ? next({ ...res, [hash]: true }) : [],
);
};
const name = ["ball", "bat", "glove", "glove", "glove"];
const price = [2, 3, 1, 2, 1];
const weight = [2, 5, 1, 1, 1];
console.log(
findDupes(name, price, weight),
);
Javascript 没有 HashSet
,但有一个 Set
可以存储任何你喜欢的东西。因此,要测试重复项:
创建一个空的
Set
遍历
i = 0..items.length
对于每个
i
,从name[i]
、price[i]
和weight[i]
创建一个散列。注意:你怎么做并不重要,只要满足以下条件(注意 3x===
):hash(name[i], price[i], weight[i]) === hash(name[i], price[i], weight[i])
创建哈希后,检查它是否已经存在于您的
Set
- 如果存在,则存在重复,您可以立即return
- 如果没有,则将散列添加到集合中并继续循环
不提前到达循环末尾return意味着没有重复
let name, price, weight;
const makeHash = (name, price, weight) =>
`${name}__${price}__${weight}`;
const duplicateTest = () => {
const seen = new Set();
for (let i = 0; i < name.length; i += 1) {
const hash = makeHash(name[i], price[i], weight[i]);
if (seen.has(hash)) return true;
seen.add(hash);
}
return false;
}
name = ["ball", "bat", "glove", "glove", "glove"]
price = [2, 3, 1, 2, 1]
weight = [2, 5, 1, 1, 1]
console.log(duplicateTest()); // Output: true
name = ["ball", "bat", "glove", "glove", "glove"]
price = [2, 3, 1, 2, 2]
weight = [2, 5, 1, 1, 2]
console.log(duplicateTest()); // Output: false