我需要检查一个JavaScript数组,看看是否有任何重复的值。最简单的方法是什么?我只需要找到复制的值是什么——实际上我不需要它们的索引,也不需要它们复制了多少次。
我知道我可以循环遍历数组并检查所有其他值是否匹配,但似乎应该有更简单的方法。
类似的问题:
获取JavaScript数组中所有唯一的值(删除重复值)
我需要检查一个JavaScript数组,看看是否有任何重复的值。最简单的方法是什么?我只需要找到复制的值是什么——实际上我不需要它们的索引,也不需要它们复制了多少次。
我知道我可以循环遍历数组并检查所有其他值是否匹配,但似乎应该有更简单的方法。
类似的问题:
获取JavaScript数组中所有唯一的值(删除重复值)
当前回答
这是我能想到的最简单的解决办法:
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)
希望这能有所帮助。
其他回答
返回副本并保留数据类型。
具有O(4n)性能
const dupes = arr => {
const map = arr.reduce((map, curr) => {
return (map.set(curr, (map.get(curr) || 0) + 1), map)
}, new Map());
return Array.from(map).filter(([key, val])=> val > 1).map(([key, val]) => key)
}
具有O(2n)性能
const dupes = arr => {
const map = arr.reduce((map, curr) => {
return (map.set(curr, (map.get(curr) || 0) + 1), map)
}, new Map());
const dupes_ = [];
for (let [key, val] of map.entries()) {
if (val > 1) dupes_.push(key);
}
return dupes_;
}
更新:简短的一行程序,以获得副本:
[1, 2, 2, 4, 3, 4].filter((e, i, a) => a.indexOf(e) !== i) // [2, 4]
要获得没有重复项的数组,只需反转条件:
[1, 2, 2, 4, 3, 4].filter((e, i, a) => a.indexOf(e) === i) // [1, 2, 3, 4]
注意,这个答案的主要目标是简短。如果你需要一个大数组的性能,一个可能的解决方案是先排序你的数组(如果它是可排序的),然后执行以下操作来获得与上面相同的结果:
myHugeSortedArray.filter((e, i, a) => a[i-1] === e)
下面是一个1 000 000个整数数组的例子:
const myHugeIntArrayWithDuplicates =
[...Array(1_000_000).keys()]
// adding two 0 and four 9 duplicates
.fill(0, 2, 4).fill(9, 10, 14)
console.time("time")
console.log(
myHugeIntArrayWithDuplicates
// a possible sorting method for integers
.sort((a, b) => a > b ? 1 : -1)
.filter((e, i, a) => a[i-1] === e)
)
console.timeEnd("time")
在我的AMD Ryzen 7 5700G开发机上输出:
[ 0, 0, 9, 9, 9, 9 ]
time: 22.738ms
正如在评论中指出的那样,短解决方案和性能解决方案都将返回一个具有多次相同副本的数组,如果它在原始数组中出现多次:
[1, 1, 1, 2, 2, 2, 2].filter((e, i, a) => a.indexOf(e) !== i) // [1, 1, 2, 2, 2]
如果需要唯一的副本,则函数为
function duplicates(arr) {
return [...new Set(arr.filter((e, i, a) => a.indexOf(e) !== i))]
}
可以使用duplicate([1, 1, 1, 2, 2, 2, 2])返回[1,2]。
当你所需要的只是检查是否像这个问题中要求的那样没有重复时,你可以使用every()方法:
[1, 2, 3].every((e, i, a) => a.indexOf(e) === i) // true
[1, 2, 1].every((e, i, a) => a.indexOf(e) === i) // false
注意,every()在ie8及以下版本中不起作用。
你可以通过比较index:
function getDuplicate(array) {
return array.filter((value, index) => array.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;
}
类似于其他一些答案,但我使用forEach()使它更漂亮一点:
function find_duplicates(data) {
var track = {};
var duplicates = [];
data.forEach(function (item) {
!track[item] ? track[item] = true : duplicates.push(item);
});
return duplicates;
}
如果一个值被复制了不止一次,它的所有副本将被返回,如下所示:
find_duplicates(['foo', 'foo', 'bar', 'bar', 'bar']);
// returns ['foo', 'bar', 'bar']
这可能就是你想要的,否则你只能使用一个“唯一的”过滤。