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

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

类似的问题:

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


当前回答

你可以通过比较index:

function getDuplicate(array) {
    return array.filter((value, index) => array.value !== index)
}

其他回答

已经有很多答案了,但不幸的是,有些太长了,有些太短了,但对我来说太神秘了,而另一些则超出了我的知识范围……不过,我真的很喜欢我提出的这个解决方案。希望它仍然对一些人有帮助!

尽管最初的帖子说他/她实际上不需要重复的索引,也不需要重复多少次,但我认为仍然需要清楚地计算它们。

带有注释的代码。

function findDuplicates(array, count = {}) {
  // with count declared in the parameter, initialized as an empty object, 
  // it can store the counts of all elements in array  
  
  // using the forEach loop to iterate through the input array, 
  // also using the conditional ternary operators 
  // (works just like a normal if-else statement, but just a bit cleaner)
  // we can store all occurrences of each element from array in count
  array.forEach(el => count[el] ? count[el]++ : count[el] = 1)
  
  // using Object.keys, we get an array of all keys from count (all numbers) 
  // (sorted as well, though of no specific importance here)
  // using filter to find all elements with a count (value) > 1 (duplicates!)
  return Object.keys(count).filter(key => count[key] > 1);
}

只有代码(带有测试用例)。

函数findduplicate(数组,count = {}) { 数组中。forEach(el => count[el] ?Count [el]++: Count [el] = 1); 返回种(计数)。Filter (key => count[key] > 1); } 让arr1 = [9,9,111, 2,3,4,4,5,7]; 让arr2 = [1,6,7,3,6,8,1,3,4,5,1,7,2,6]; console.log (findDuplicates (arr1));// => ['4', '9'] console.log (findDuplicates (arr2));// => ['1', '3', '6', '7']

下面是一个简单的小片段,用于查找唯一的和重复的值,无需排序和两个循环。

Var _unique =函数(arr) { Var h = [], t = []; 加勒比海盗。forEach(函数(n) { if (h.indexOf(n) == -1) h.push (n); 其他t.push (n); }); 返回[h, t]; } var =结果_unique([“测试”,1 4 2,34岁,6日,21日,3,4,“测试”、“王子”、“th”,34]); console.log("Has duplicate values: " + (result[1]. log)长度> 0))//你可以检查重复值的计数 Console.log (result[0]) //唯一值 Console.log (result[1]) //重复值

//program to find the duplicate elements in arraylist

import java.util.ArrayList;
import java.util.Scanner;

public class DistinctEle 
{ 
    public static void main(String args[])
    {
        System.out.println("Enter elements");
        ArrayList<Integer> abc=new ArrayList<Integer>();
        ArrayList<Integer> ab=new ArrayList<Integer>();
        Scanner a=new Scanner(System.in);
        int b;
        for(int i=0;i<=10;i++)
        {
            b=a.nextInt();
            if(!abc.contains(b))
            {
                abc.add(b);
            }
            else
            {
                System.out.println("duplicate elements"+b);
            }
        }
    }
}

使用underscore.js

function hasDuplicate(arr){
    return (arr.length != _.uniq(arr).length);
}

使用“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 >