需要在多维数组中查找或计算重复项 [英] Need to find or count duplicates in multidimensional array
问题描述
我需要计算多维数组中重复项的数量,并在发现重复项时发出警报.
I need to count the no of duplicates in an multidimensional array and give alert if duplicates found.
Arr =[[2,sk"],[3,df"],[7,uz"],[3,df"],[7,gh"]]
Arr =[[2,"sk"],[3,"df"],[7,"uz"],[3,"df"],[7,"gh"]]
建议:计数可以以这种方式完成,如果 arr[0] 位置等于下一个位置,那么它必须给出一个计数 &需要检查两个值的组合是否相同.然后为 arr[1] 位置检查下一个位置,依此类推其他位置直到最后
Suggestions: Count can be done in this manner that if arr[0] position is equals to next coming positions then it must give a count & needs to check both values combination same. And then for arr[1] position to check for next coming positions and so on for other position till last
只计算精确的组合喜欢[3,df"]在第二个位置等于[3,df"] 在第四个组合
Only counts the exact combination Like [3,"df"] at second position equal to [3,"df"] at fourth combination
预期输出计数:1 发现重复数据警报
Expected Output Count :1 Alert duplicate data found
推荐答案
我的 answer 来自原始问题(除了那个我在输入数组中有三个项目,如 [3, "df"]
是:
My answer from the original question (except that I've got three items like [3, "df"]
in the input array) is:
const input = [
[2, "sk"], [3, "df"], [7, "uz"], [3, "df"], [7, "gh"],
[5, "df"], [21, "sk"], [2, "1sk"], [3, "df"]
];
const duplicate_count = input.length - new Set( input.map(JSON.stringify) ).size;
console.log(duplicate_count);
如果 [[3,"df"],[3,"df"],[3,"df"]]
的计数应该是一而不是二,那么也许类似:
If the count for [[3,"df"],[3,"df"],[3,"df"]]
should be one instead of two then perhaps something like:
const input = [
[2, "sk"], [3, "df"], [7, "uz"], [3, "df"], [7, "gh"],
[5, "df"], [21, "sk"], [2, "1sk"], [3, "df"]
];
const duplicate_count = [
...input
.map(JSON.stringify)
.reduce( (acc, v) => acc.set(v, (acc.get(v) || 0) + 1), new Map() )
.values()
].filter((v) => v > 1).length;
console.log(duplicate_count);
这篇关于需要在多维数组中查找或计算重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!