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

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

类似的问题:

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


当前回答

这是我的简单和一行解决方案。

它首先搜索不是唯一的元素,然后使用Set使所找到的数组是唯一的。

最后我们有一个重复的数组。

Var数组= [1,2,2,3,3,4,5,6,2,3,7,8,5,22,1,2,511,12,50,22]; console.log([…新设置( 数组中。filter((value, index, self) => self. indexof (value) !== index))] );

其他回答

有一个非常简单的方法来解决这个问题。如果你使用新的'Set' javascript命令。Set可以接受一个数组作为输入,并输出一个只包含唯一值的新“Set”。然后通过比较数组的长度和集合的'size'属性,你可以看到它们是否不同。如果它们不同,一定是由于重复的条目。

Var array1 = ['value1','value2','value3','value1'];//包含重复项 Var array2 = ['value1','value2','value3','value4'];//唯一值 console.log('array1 contains duplicate = ' + containsduplicate (array1)); console.log('array2 contains duplicate = ' + containsduplicate (array2)); 函数containsduplicate (passedArray) { let mySet = new Set(passsedarray); 如果(mySet。size == passsedarray .length) { 返回true; } 返回错误; }

如果运行上面的代码片段,将得到以下输出。

Array1包含duplicate = true

Array2包含重复项= false

我试过了,你会得到唯一的元素和在两个不同数组中重复的元素。

复杂度O (n)

let start = [1,1,2,1,3,4,5,6,5,5]; start.sort(); const unique=[]; const repeat = []; let ii=-1 ; for(let i =0 ; i<start.length; i++){ if(start[i]===start[i-1]){ if(repeat[ii]!==start[i-1]){ repeat.push(start[i-1]); ii++; } } else { if(i+1<start.length){ if(start[i]!==start[i+1]){ unique.push(start[i]); } } else if(i===start.length-1){ unique.push(start[i]); } } } console.log(unique) ; console.log(repeat);

这是我能想到的最简单的解决办法:

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)

希望这能有所帮助。

公认的答案是最完美的,但正如一些用户指出的那样,对于一个元素重复超过2次的情况,它将给出具有重复元素的数组:

这个解决方案也涵盖了这些场景:

const peoples = [
  {id: 1, name:"Arjun"},
  {id: 2, name:"quinze"},
  {id: 3, name:"catorze"},
  {id: 1, name:"Arjun"},
  {id: 4, name:"dezesseis"},
  {id: 1, name:"Arjun"},
  {id: 2, name:"quinze"},
  {id: 3, name:"catorzee"}
]


function repeated(ppl){

  const newppl = ppl.slice().sort((a,b) => a.id -b.id);

  let rept = [];
  for(let i = 0; i < newppl.length-1 ; i++){
    if (newppl[i+1].id == newppl[i].id){
      rept.push(newppl[i+1]);
    }
  }

  return [...new Set(rept.map(el => el.id))].map(rid => 
    rept.find(el => el.id === rid)
  );

}

repeated(peoples);

这是我的建议(ES6):

let a = [1, 2, 3, 4, 2, 2, 4, 1, 5, 6]
let b = [...new Set(a.sort().filter((o, i) => o !== undefined && a[i + 1] !== undefined && o === a[i + 1]))]

// b is now [1, 2, 4]