我需要检查一个JavaScript数组,看看是否有任何重复的值。最简单的方法是什么?我只需要找到复制的值是什么——实际上我不需要它们的索引,也不需要它们复制了多少次。
我知道我可以循环遍历数组并检查所有其他值是否匹配,但似乎应该有更简单的方法。
类似的问题:
获取JavaScript数组中所有唯一的值(删除重复值)
我需要检查一个JavaScript数组,看看是否有任何重复的值。最简单的方法是什么?我只需要找到复制的值是什么——实际上我不需要它们的索引,也不需要它们复制了多少次。
我知道我可以循环遍历数组并检查所有其他值是否匹配,但似乎应该有更简单的方法。
类似的问题:
获取JavaScript数组中所有唯一的值(删除重复值)
当前回答
您可以对数组进行排序,然后遍历它,然后查看下一个(或上一个)索引是否与当前索引相同。假设你的排序算法是好的,这个应该小于O(n2):
const findDuplicates = (arr) => { let sorted_arr = arr.slice().sort(); // You can define the comparing function here. // JS by default uses a crappy string compare. // (we use slice to clone the array so the // original array won't be modified) let results = []; for (let i = 0; i < sorted_arr.length - 1; i++) { if (sorted_arr[i + 1] == sorted_arr[i]) { results.push(sorted_arr[i]); } } return results; } let duplicatedArray = [9, 9, 111, 2, 3, 4, 4, 5, 7]; console.log(`The duplicates in ${duplicatedArray} are ${findDuplicates(duplicatedArray)}`);
在这种情况下,如果你要返回一个重复的函数。这是为类似类型的情况。
参考:https://stackoverflow.com/a/57532964/8119511
其他回答
这是我的简单和一行解决方案。
它首先搜索不是唯一的元素,然后使用Set使所找到的数组是唯一的。
最后我们有一个重复的数组。
Var数组= [1,2,2,3,3,4,5,6,2,3,7,8,5,22,1,2,511,12,50,22]; console.log([…新设置( 数组中。filter((value, index, self) => self. indexof (value) !== index))] );
var a = [324,3,32,5,52,2100,1,20,2,3,3,2,2,2,1,1,1].sort();
a.filter(function(v,i,o){return i&&v!==o[i-1]?v:0;});
或者当添加到原型时。阵列链
//copy and paste: without error handling
Array.prototype.unique =
function(){return this.sort().filter(function(v,i,o){return i&&v!==o[i-1]?v:0;});}
请看这里:https://gist.github.com/1305056
/* Array对象的indexOf方法用于比较数组项。 IE是唯一一个原生不支持它的主流浏览器,但它很容易实现: * /
Array.prototype.indexOf= Array.prototype.indexOf || function(what, i){
i= i || 0;
var L= this.length;
while(i<L){
if(this[i]=== what) return i;
++i;
}
return -1;
}
function getarrayduplicates(arg){
var itm, A= arg.slice(0, arg.length), dups= [];
while(A.length){
itm= A.shift();
if(A.indexOf(itm)!= -1 && dups.indexOf(itm)== -1){
dups[dups.length]= itm;
}
}
return dups;
}
var a1= [1, 22, 3, 2, 2, 3, 3, 4, 1, 22, 7, 8, 9];
警报(getarrayduplicates (a1));
对于非常大的数组,可以更快地从数组中删除找到的重复项,这样就不会再次查看它们:
function getarrayduplicates(arg){
var itm, A= arg.slice(0, arg.length), dups= [];
while(A.length){
itm= A.shift();
if(A.indexOf(itm)!= -1){
dups[dups.length]= itm;
while(A.indexOf(itm)!= -1){
A.splice(A.indexOf(itm), 1);
}
}
}
return dups;
}
我认为下面是完成你要求的最简单和最快的O(n)方法:
function getDuplicates( arr ) {
var i, value;
var all = {};
var duplicates = [];
for( i=0; i<arr.length; i++ ) {
value = arr[i];
if( all[value] ) {
duplicates.push( value );
all[value] = false;
} else if( typeof all[value] == "undefined" ) {
all[value] = true;
}
}
return duplicates;
}
对于ES5或更高版本:
function getDuplicates( arr ) {
var all = {};
return arr.reduce(function( duplicates, value ) {
if( all[value] ) {
duplicates.push(value);
all[value] = false;
} else if( typeof all[value] == "undefined" ) {
all[value] = true;
}
return duplicates;
}, []);
}
你可以使用下面的代码来获取给定数组中的重复元素:
let name = ['satya', 'amit', 'aditya', 'abhay', 'satya', 'amit'];
let dup = [];
let uniq = [];
name.forEach((item, index) => {
if(!uniq.includes(item)) {
uniq[index] = item;
}
if (name.indexOf(item, index + 1) != -1) {
dup[index] = item;
}
})