fastest way to detect if duplicate entry exists in javascript array?

前端 未结 8 2024
时光说笑
时光说笑 2021-02-06 09:54
var arr = [\'test0\',\'test2\',\'test0\'];

Like the above,there are two identical entries with value \"test0\",how to check it most efficiently?

8条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-06 10:31

    If you sort the array, the duplicates are next to each other so that they are easy to find:

    arr.sort();
    var last = arr[0];
    for (var i=1; i

提交回复
热议问题