我需要检查一个JavaScript数组,看看是否有任何重复的值。最简单的方法是什么?我只需要找到复制的值是什么——实际上我不需要它们的索引,也不需要它们复制了多少次。
我知道我可以循环遍历数组并检查所有其他值是否匹配,但似乎应该有更简单的方法。
类似的问题:
获取JavaScript数组中所有唯一的值(删除重复值)
我需要检查一个JavaScript数组,看看是否有任何重复的值。最简单的方法是什么?我只需要找到复制的值是什么——实际上我不需要它们的索引,也不需要它们复制了多少次。
我知道我可以循环遍历数组并检查所有其他值是否匹配,但似乎应该有更简单的方法。
类似的问题:
获取JavaScript数组中所有唯一的值(删除重复值)
当前回答
var a = ["a","a","b","c","c"];
a.filter(function(value,index,self){ return (self.indexOf(value) !== index )})
其他回答
/* 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']
这应该是在数组中查找重复值的最短和最简单的方法之一。
Var arr = [1,2,3,4,5,6,7,8,1,2,3,4,5,3,4]; Var数据= arr.filter(函数(项目,索引,arr){ 返回arr.indexOf(item) != arr.lastIndexOf(item) && arr.indexOf(item) == index; }) console.log(数据);
我只需要找到复制的值是什么——实际上我不需要它们的索引,也不需要它们复制了多少次。
这是一个有趣而简单的任务,有许多难以阅读的答案……
打印稿
function getDuplicatedItems<T>(someArray: T[]): T[] {
// create a set to iterate through (we only need to check each value once)
const itemSet = new Set<T>(someArray);
// from that Set, we check if any of the items are duplicated in someArray
const duplicatedItems = [...itemSet].filter(
(item) => someArray.indexOf(item) !== someArray.lastIndexOf(item)
);
return duplicatedItems;
}
JavaScript
function getDuplicatedItems(someArray) {
// check for misuse if desired
// if (!Array.isArray(someArray)) {
// throw new TypeError(`getDuplicatedItems requires an Array type, received ${typeof someArray} type.`);
// }
const itemSet = new Set(someArray);
const duplicatedItems = [...itemSet].filter(
(item) => someArray.indexOf(item) !== someArray.lastIndexOf(item)
);
return duplicatedItems;
}
我认为下面是完成你要求的最简单和最快的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;
}, []);
}