我需要检查一个JavaScript数组,看看是否有任何重复的值。最简单的方法是什么?我只需要找到复制的值是什么——实际上我不需要它们的索引,也不需要它们复制了多少次。
我知道我可以循环遍历数组并检查所有其他值是否匹配,但似乎应该有更简单的方法。
类似的问题:
获取JavaScript数组中所有唯一的值(删除重复值)
我需要检查一个JavaScript数组,看看是否有任何重复的值。最简单的方法是什么?我只需要找到复制的值是什么——实际上我不需要它们的索引,也不需要它们复制了多少次。
我知道我可以循环遍历数组并检查所有其他值是否匹配,但似乎应该有更简单的方法。
类似的问题:
获取JavaScript数组中所有唯一的值(删除重复值)
当前回答
非常简单的方法:
函数getDuplicateValues(someArray) { const duplicateValues = new Set([]) const check = new Set([]) someArray。forEach(v => { If (check.has(v)) { duplicateValues.add (v) }其他{ check.add (v) } }) 返回Array.from (duplicateValues); } const result = getDuplicateValues(['咖啡','苏打','水','果汁','水','水','咖啡']) repeated_values。textContent = JSON。Stringify(结果,null, ' ') < pre id = " repeated_values " > < / >之前
其他回答
使用underscore.js
function hasDuplicate(arr){
return (arr.length != _.uniq(arr).length);
}
这可能是从数组中永久删除重复项的最快方法之一 比大多数函数快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));
使用Pure Js
function arr(){
var a= [1,2,3,4,5,34,2,5,7,8,6,4,3,25,8,34,56,7,8,76,4,23,34,46,575,8564,53,5345657566];
var b= [];
b.push(a[0]);
var z=0;
for(var i=0; i< a.length; i++){
for(var j=0; j< b.length; j++){
if(b[j] == a[i]){
z = 0;
break;
}
else
z = 1;
}
if(z)
b.push(a[i]);
}
console.log(b);
}
这是我能想到的最简单的解决办法:
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(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)