如何在javascript中查找重复值并将重复值存储到新数组中

How to find repeated values and store repeated values into new array in javascript

提问人:Jonas 提问时间:9/14/2021 更新时间:9/14/2021 访问量:130

问:

我正在尝试实现一个逻辑,其中我有一个数组。我想找到所有重复的元素,我想将这些重复的元素存储到一个新数组中。我非常努力,但没有找到解决方案。[3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6]

如果有人编写简单的代码并解释代码的作用,那就太好了。

谢谢

JavaScript 数组 ecmascript-2017

评论

1赞 evolutionxbox 9/14/2021
你能分享你尝试过的代码吗?
1赞 9/14/2021
我发现 3 种可能的解决方案不到 1 分钟,你尝试了什么?
0赞 Jonas 9/14/2021
我是初学者,我不知道发生了什么
0赞 Andy 9/14/2021
如果您不将其添加到问题中,我们将无法帮助调试您的代码。欢迎来到 SO。您可能会发现阅读网站帮助部分提出好问题此问题清单时很有用。你为解决问题而编写的代码应该包括一个最小的可重现示例,并包含在你的问题中。
2赞 Harshit Rastogi 9/14/2021
这回答了你的问题吗?获取 JavaScript 数组中的所有唯一值(删除重复项)

答:

0赞 user4431269 9/14/2021 #1

首先,您需要了解一些基础知识。

阵列映射 - MDN 信息

数组映射创建一个新数组,并且不会更改当前数组。

//So if we do:
[3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6].map((element, index, array) => { return 0; } );
//We get
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Array.indexOf(element) MDN 信息

返回数组中元素的索引,-1 未找到,从 0 开始。

//So if we do:
[3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6].map( (element, index, array) => { return array.indexOf(element); } );
// we get 
// [0, 1, 2, 3, 3, 5, 3, 7, 2, 7, 1, 3, 12, 7, 1, 15, 16, 7, 18, 5, 1, 3, 22, 0, 12]

//so if we do use the second parameter of map in combination with indexOf
[3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6].map( (element, index, array) => { return array.indexOf(element) === index; } );
// we get 
// [true, true, true, true, false, true, false, true, false, false, false, false, true, false, false, true, true, false, true, false, false, false, true, false, false]

总结一下:

[3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6].map( (element, index, array) => { 
    if ( array.indexOf(element) !== index ) { 
       return element; 
    } else { 
        return undefined; 
    }
});
// will give us
// [undefined, undefined, undefined, undefined, 5, undefined, 5, undefined, 63, 2, 4, 5, undefined, 2, 4, undefined, undefined, 2, undefined, 1, 4, 5, undefined, 3, 6]

MDN 上的数组过滤器信息

// so now we have our array of elements that are duplicated with undefined values in it, now we filter.

[undefined, undefined, undefined, undefined, 5, undefined, 5, undefined, 63, 2, 4, 5, undefined, 2, 4, undefined, undefined, 2, undefined, 1, 4, 5, undefined, 3, 6].filter( (element) => { 
    return element !== undefined; 
});
//result
[5, 5, 63, 2, 4, 5, 2, 4, 2, 1, 4, 5, 3, 6]

这是我能给出的最基本的解释,不使用条件运算符,存储在变量中等。如果你在编程方面更高级一点,你可以编写较少的短代码,称为 1 行代码。但我希望这能给你带来灵感,让你继续下去。但是你首先要了解基本知识,这意味着学习、阅读、多学习和多练习。

评论

0赞 sp00m 9/14/2021
您可以直接在原始数组上。.filter((e, i, a) => a.indexOf(e) !== i)
0赞 sp00m 9/14/2021
哇,很简单,我只是指出中间步骤没有任何意义,因为您的最终步骤可以直接在原始数组上调用,逻辑完全相同。这不是关于单行代码,而是关于不要在数组上迭代两次。.map.filter.indexOf
0赞 9/14/2021
是的,优化你的代码,最有趣的部分是追逐那些μs。但我们都必须从某个地方开始。我怀疑这个问题会被删除。我应该删除它。但是新人对编程有一些最基本的问题,不知道该去哪里或如何开始。
0赞 Karma Blackshaw 9/14/2021
哈哈哈哈轻松的家伙
0赞 Ranjan 9/14/2021 #2

如果你只需要数组的重复项,你可以使用这个:

var arry = [3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6];
var rep_arry = [];
   
for (const [key,val] of Object.entries(arry)) {
  for (const [key1,val1] of Object.entries(arry)) {
     if(val == val1 && !rep_arry.includes(val) && key != key1){
         rep_arry.push(val);
     }
  }    
}
console.log(rep_arry);

它返回这个

[3, 4, 63, 5, 1, 2, 6]

上面的代码使用两个 for 循环,它检查每个单独的元素,每个元素使用两个 for 循环。在推送之前,我放置了 3 个条件,1 个用于检查等于,2 个用于检查项目是否可用于新数组,第三个条件用于防止使用键检查自己的元素。它的工作量也很大。请参阅代码片段。

<html>

    <script>
        var arry = [3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6];
        var rep_arry = [];
       
        for (const [key,val] of Object.entries(arry)) {
           
            for (const [key1,val1] of Object.entries(arry)) {
                if(val == val1 && !rep_arry.includes(val) && key != key1){
                    rep_arry.push(val);
                }
            }
            
        }
        console.log(rep_arry);
    </script>
</html>

评论

0赞 9/14/2021
这在一个小数组上工作,当你有 5000 个元素时,你会得到 troubels。OP 要求解释代码在做什么。
0赞 Ranjan 9/14/2021
你认为它会在数组中遇到更多麻烦吗?,
0赞 Ranjan 9/14/2021
@PhilAndelhofs,回答您的问题,请查看此 codepen.io/ranjandaswani/pen/RwgZEev
0赞 Hayk 9/14/2021 #3

我们可以通过保留唯一元素列表来解决这个问题。 如果我们保留唯一元素的列表,那么收集重复元素将变得容易。

这可能不是效率的最佳解决方案,但它解决了当前的问题。我注释了代码,所以它可能会帮助你了解发生了什么。

要解决这类问题,最好学习算法。

const arr = [3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6];

let uniqueElmts = [];  // here we gonna store unique elements
let duplicates = [];   // here we gonna store duplicate elements 

for(let i = 0; i < arr.length; i++){
   if(uniqueElmts.includes(arr[i])){  // if 'uniqueElmts' already contains arr[i], then arr[i] is a duplicate 
        duplicates.push(arr[i]);  // push arr[i] element to 'duplicates' array, because it's duplicate
   }
   else {
        uniqueElmts.push(arr[i]);     // if 'uniqueElmts' doesn't contain arr[i] then it's unique element so we push arr[i] to 'uniqueElmts' 
   }
}


console.log(duplicates); // [ 5, 5, 63, 2, 4, 5, 2, 4, 2, 1, 4, 5, 3, 6 ]