我需要检查一个JavaScript数组,看看是否有任何重复的值。最简单的方法是什么?我只需要找到复制的值是什么——实际上我不需要它们的索引,也不需要它们复制了多少次。
我知道我可以循环遍历数组并检查所有其他值是否匹配,但似乎应该有更简单的方法。
类似的问题:
获取JavaScript数组中所有唯一的值(删除重复值)
我需要检查一个JavaScript数组,看看是否有任何重复的值。最简单的方法是什么?我只需要找到复制的值是什么——实际上我不需要它们的索引,也不需要它们复制了多少次。
我知道我可以循环遍历数组并检查所有其他值是否匹配,但似乎应该有更简单的方法。
类似的问题:
获取JavaScript数组中所有唯一的值(删除重复值)
当前回答
这是我在重复线程(!)中的回答:
在2014年写这篇文章时,所有的例子都是for-loops或jQuery。JavaScript有完美的工具:排序、映射和缩减。
找到重复的物品
var名称=[“迈克”,“马特”,“南希”,“亚当”,“珍妮”,“南希”,“卡尔”) Const uniq = names .map((name) => { 返回{ 数:1 名称:名称 }; }) .reduce((result, b) => { Result [b.name] = (Result [b.name] || 0) + b.count; 返回结果; }, {}); const duplicate = Object.keys(uniq).filter((a) => uniq[a] > 1); console.log(副本);// ['Nancy']
更多函数式语法:
@Dmytro-Laptin指出了一些可以删除的代码。这是相同代码的一个更紧凑的版本。使用一些ES6技巧和高阶函数:
常量名称=[“迈克”,“马特”,“南希”,“亚当”,“珍妮”,“南希”,“卡尔”); Const count = names => 的名字。Reduce ((result, value) =>({… [value]:(result[value] || 0) + 1 }, {});//不要忘记初始化累加器 Const duplicate = dict => Object.keys(dict).filter((a) => dict[a] > 1); console.log (count(名称));//{迈克:1,马特:1,南希:2,亚当:1,珍妮:1,卡尔:1} console.log(副本(count(名字)));// ['Nancy']
其他回答
这是我用map实现的。它应该运行在O(n)时间,应该有点容易喘气。
var first_array = [1, 1, 2, 3, 4, 4, 5, 6); var find_dup=new Map; For (first_array的const迭代器){ //如果现值++ 如果(find_dup.has (iterator)) { find_dup.set(迭代器,find_dup.get(迭代器)+ 1); 其他}{ //否则添加 find_dup.set(迭代器,1); } } console.log (find_dup.get (2));
然后你可以find_dup.get(key)来查找它是否有重复(它应该给> 1)。
简单的一行方式
Var arr = [9,1,2,4,3,4,9] console.log(arr.filter((ele,indx)=>indx!==arr.indexOf(ele))) //获取副本 console.log(arr.filter((ele,indx)=>indx===arr.indexOf(ele))) //删除重复项
这是我能想到的最简单的解决办法:
const arr =[1、2、2、2 0,0,0,500,1,“,”“,”“) Const filtered = arr。filter((el, index) => arr.indexOf(el) !== index) // => filtered = [2,2,0,0, -1, 'a', 'a'] Const duplicate =[…]新的(过滤) console.log(副本) // => [2,0, -1, 'a']
就是这样。
注意:
It works with any numbers including 0, strings and negative numbers e.g. -1 - Related question: Get all unique values in a JavaScript array (remove duplicates) The original array arr is preserved (filter returns the new array instead of modifying the original) The filtered array contains all duplicates; it can also contain more than 1 same value (e.g. our filtered array here is [ 2, 2, 0, 0, -1, 'a', 'a' ]) If you want to get only values that are duplicated (you don't want to have multiple duplicates with the same value) you can use [...new Set(filtered)] (ES6 has an object Set which can store only unique values)
希望这能有所帮助。
//find duplicates: //sort, then reduce - concat values equal previous element, skip others //input var a = [1, 2, 3, 1, 2, 1, 2] //short version: var duplicates = a.sort().reduce((d, v, i, a) => i && v === a[i - 1] ? d.concat(v) : d, []) console.log(duplicates); //[1, 1, 2, 2] //readable version: var duplicates = a.sort().reduce((output, element, index, input) => { if ((index > 0) && (element === input[index - 1])) return output.concat(element) return output }, []) console.log(duplicates); //[1, 1, 2, 2]
在一次采访中有人问我这个问题,我的回答是,
List<int> getDublicates(List<int> x)
{
List<int> result = new List<int>();
while (x.Count>0)
{
int d = x[0];
x.Remove(x[0]);
if (x.Contains(d))
result.Add(d);
}
return result;
}
它的性能很好