需要在多维数组中查找或计算重复项 [英] Need to find or count duplicates in multidimensional array

查看:26
本文介绍了需要在多维数组中查找或计算重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算多维数组中重复项的数量,并在发现重复项时发出警报.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆