我需要检查一个JavaScript数组,看看是否有任何重复的值。最简单的方法是什么?我只需要找到复制的值是什么——实际上我不需要它们的索引,也不需要它们复制了多少次。

我知道我可以循环遍历数组并检查所有其他值是否匹配,但似乎应该有更简单的方法。

类似的问题:

获取JavaScript数组中所有唯一的值(删除重复值)


当前回答

ES6语法的简单代码(返回重复的排序数组):

let duplicates = a => {d=[]; a.sort((a,b) => a-b).reduce((a,b)=>{a==b&&!d.includes(a)&&d.push(a); return b}); return d};

使用方法:

duplicates([1,2,3,10,10,2,3,3,10]);

其他回答

var a = ["a","a","b","c","c"];

a.filter(function(value,index,self){ return (self.indexOf(value) !== index )})

//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]

/* 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;
}

快速和优雅的方式使用es6对象解构和减少

它在O(n)(对数组进行1次迭代)中运行,并且不会重复出现超过2次的值

const arr =['你好','嗨',“你好”,“再见”,“再见”,“自闭症”) const { dup } = arr.reduce( (acc, curr) => { acc。Items [curr] = acc。项目(咕咕叫)?acc。项目[curr] += 1: 1 如果(acc)。项目[curr] === 2) acc.dup.push(curr) 返回acc }, { 项目:{}, dup: [] }, ) console.log (dup) // ['hi', 'bye']

这可能是从数组中永久删除重复项的最快方法之一 比大多数函数快10倍。safari快78倍

function toUnique(a,b,c){//array,placeholder,placeholder
 b=a.length;
 while(c=--b)while(c--)a[b]!==a[c]||a.splice(c,1)
}
var array=[1,2,3,4,5,6,7,8,9,0,1,2,1];
toUnique(array);
console.log(array);

测试:http://jsperf.com/wgu 演示:http://jsfiddle.net/46S7g/ 更多:https://stackoverflow.com/a/25082874/2450730

如果你不能阅读上面的代码,请阅读javascript书籍,或者这里有一些关于较短代码的解释。https://stackoverflow.com/a/21353032/2450730

编辑 正如注释中所述,此函数确实返回一个具有惟一值的数组,但是问题要求查找重复项。在这种情况下,对这个函数进行简单的修改就可以将重复项推入数组,然后使用前面的函数来移除重复项的重复项。

function theDuplicates(a,b,c,d){//array,placeholder,placeholder
 b=a.length,d=[];
 while(c=--b)while(c--)a[b]!==a[c]||d.push(a.splice(c,1))
}
var array=[1,2,3,4,5,6,7,8,9,0,1,2,1];

toUnique(theDuplicates(array));