我需要检查一个JavaScript数组,看看是否有任何重复的值。最简单的方法是什么?我只需要找到复制的值是什么——实际上我不需要它们的索引,也不需要它们复制了多少次。
我知道我可以循环遍历数组并检查所有其他值是否匹配,但似乎应该有更简单的方法。
类似的问题:
获取JavaScript数组中所有唯一的值(删除重复值)
我需要检查一个JavaScript数组,看看是否有任何重复的值。最简单的方法是什么?我只需要找到复制的值是什么——实际上我不需要它们的索引,也不需要它们复制了多少次。
我知道我可以循环遍历数组并检查所有其他值是否匹配,但似乎应该有更简单的方法。
类似的问题:
获取JavaScript数组中所有唯一的值(删除重复值)
当前回答
在数组中查找重复的值
这应该是在数组中找到重复值的最短方法之一。正如OP特别要求的那样,这不会删除重复项,而是找到它们。
Var输入= [1,2,3,1,3,1]; Var duplicate =输入。Reduce(函数(acc, el, i, arr) { if (arr.indexOf(el) !== i && ac . indexof (el) < 0) ac .push(el);返回acc; },[]); document . write(副本);// = 1,3(实际数组= [1,3])
这不需要排序或任何第三方框架。它也不需要手动循环。它适用于indexOf()(或者更清楚地说:严格比较运算符)支持的所有值。
因为reduce()和indexOf(),它至少需要ie9。
其他回答
Prototype库有一个uniq函数,它返回不包含dupes的数组。但这只是工作的一半。
您可以对数组进行排序,然后遍历它,然后查看下一个(或上一个)索引是否与当前索引相同。假设你的排序算法是好的,这个应该小于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
使用“includes”测试元素是否已经存在。
Var arr = [1,1,4,5,5], darr = [], duplicate = []; For (var I = 0;I < arrr .length;我+ +){ If (darr.includes(arr[i]) && !重复。includes(arr[i])) duplicates.push (arr[我]) 其他的 darr.push (arr[我]); } console.log(副本); <h3>重复数组</h3> <p>[1,1,4,5,5]</p> <h3>具有不同元素的数组</h3> <p>[1,4,5]</p> <h3>重复值</h3> (1、5)< p > < / p >
http://jsfiddle.net/vol7ron/gfJ28/
var arr = ['hello','goodbye','foo','hello','foo','bar',1,2,3,4,5,6,7,8,9,0,1,2,3];
var hash = [];
// build hash
for (var n=arr.length; n--; ){
if (typeof hash[arr[n]] === 'undefined') hash[arr[n]] = [];
hash[arr[n]].push(n);
}
// work with compiled hash (not necessary)
var duplicates = [];
for (var key in hash){
if (hash.hasOwnProperty(key) && hash[key].length > 1){
duplicates.push(key);
}
}
alert(duplicates);
The result will be the hash array, which will contain both a unique set of values and the position of those values. So if there are 2 or more positions, we can determine that the value has a duplicate. Thus, every place hash[<value>].length > 1, signifies a duplicate. hash['hello'] will return [0,3] because 'hello' was found in node 0 and 3 in arr[]. Note: the length of [0,3] is what's used to determine if it was a duplicate. Using for(var key in hash){ if (hash.hasOwnProperty(key)){ alert(key); } } will alert each unique value.
以O(n)时间复杂度(不排序)求解上述问题。
var arr = [9, 9, 111, 2, 3, 4, 4, 5, 7];
var obj={};
for(var i=0;i<arr.length;i++){
if(!obj[arr[i]]){
obj[arr[i]]=1;
} else {
obj[arr[i]]=obj[arr[i]]+1;
}
}
var result=[]
for(var key in obj){
if(obj[key]>1){
result.push(Number(key)) // change this to result.push(key) to find duplicate strings in an array
}
}
console.log(result)