如何从多个数组中复制不重复的元素

Posted

技术标签:

【中文标题】如何从多个数组中复制不重复的元素【英文标题】:how to copy non duplicate elements from multiple arrays 【发布时间】:2021-08-21 09:35:03 【问题描述】:

如何从多个动态数组中复制不重复的元素

我想让每个 graphmonthdata 的长度相同。 例如 graphmonthdata[2] 长度为 4,所以我想让它的长度以嵌套数组中的最高长度为准,而另一个嵌套数组中缺少值,反之亦然。 基本上所有嵌套数组都应该具有相同的长度和相同的元素,方法是从另一个嵌套数组中添加缺失的元素

javascript 或 c# 中请帮助谢谢!

示例嵌套数组

 graphmonthdata = [
    [
        "Bug Fixing",
        "Deployment",
        "Design",
        "Development",
        "Documentation",
        "Downtime",
        "Learning & Skill Upgrade",
        "Meetings",
        "Project Management",
        "Requirement Understanding",
        "Review",
        "Support",
        "Testing"
    ],
    [
        "Bug Fixing",
        "Deployment",
        "Development",
        "Documentation",
        "Meetings",
        "Review",
        "Support",
        "Testing",
        "UAT/Prod. Fixes"
    ],
    [
        "Meetings",
        "Organisational Activity",
        "Planned/Unplanned Leave",
        "Testing"
    ]
]

最终的数组应该是这样的


 graphmonthdata = 
[
    [
        "Bug Fixing",
        "Deployment",
        "Design",
        "Development",
        "Documentation",
        "Downtime",
        "Learning & Skill Upgrade",
        "Meetings",
        "Organisational Activity",
        "Planned/Unplanned Leave",
        "Project Management",
        "Requirement Understanding",
        "Review",
        "Support",
        "Testing",
        "UAT/Prod. Fixes"
    ],
    [
        "Bug Fixing",
        "Deployment",
        "Design",
        "Development",
        "Documentation",
        "Downtime",
        "Learning & Skill Upgrade",
        "Meetings",
        "Organisational Activity",
        "Planned/Unplanned Leave",
        "Project Management",
        "Requirement Understanding",
        "Review",
        "Support",
        "Testing",
        "UAT/Prod. Fixes"
    ],
    [
        "Bug Fixing",
        "Deployment",
        "Design",
        "Development",
        "Documentation",
        "Downtime",
        "Learning & Skill Upgrade",
        "Meetings",
        "Organisational Activity",
        "Planned/Unplanned Leave",
        "Project Management",
        "Requirement Understanding",
        "Review",
        "Support",
        "Testing",
        "UAT/Prod. Fixes"
    ]
]

【问题讨论】:

使用Set。 如果最大的内部数组不包含 smaller 数组中的项目怎么办。例如。 [['a','b','c'], ['a'], ['b'], ['d']]。在这种情况下应该是什么输出? @YevgenGorbunkov 它应该填充较小数组中的缺失值,例如这里最长的数组是 graphmonthdata[0] 没有来自 graphmonthdata[2] 的值,即“Planned/Unplanned Leave”所以我们最长的需要加上“Planned/Unplanned Leave”。希望这可以消除您的疑问 如果您需要从所有嵌套数组中获取所有唯一值,只需使用SetArray.prototype.flat()const uniqueValues = [...new Set(graphmonthdata.flat())] 【参考方案1】:

您可以使用Set 创建所有唯一值的列表。然后只需 map 覆盖具有唯一值的现有数组。

const graphmonthdata = [["Bug Fixing","Deployment","Design","Development","Documentation","Downtime","Learning & Skill Upgrade","Meetings","Project Management","Requirement Understanding","Review","Support","Testing"],["Bug Fixing","Deployment","Development","Documentation","Meetings","Review","Support","Testing","UAT/Prod. Fixes"],["Meetings","Organisational Activity","Planned/Unplanned Leave","Testing"]];

const uniqueItems = new Set(graphmonthdata.flat());
const result = graphmonthdata.map(() => [...uniqueItems]);

console.log(result);

【讨论】:

以上是关于如何从多个数组中复制不重复的元素的主要内容,如果未能解决你的问题,请参考以下文章

Java数组去重复问题

从Ruby中的数组中删除重复元素

不使用 pop() 从数组中删除最后一个元素 [重复]

如何从数组中随机选择四个元素而不用Java重复?

jquery 向空数组中添加元素,重复的就不添加到空数组中请问该如何编写

每日一题之LeetCode移除元素 删除有序数组重复元素