find duplicate

Benchmark created on


Setup


Test runner

Ready to run.

Testing in
TestOps/sec
code 2
function findDuplicatesFilter(arr) {
  return arr.filter((value, index) => arr.indexOf(value) !== index);
}

const myArray = [1, 2, 3, 2, 4, 3];
console.log(findDuplicatesFilter(myArray)); // Output: [2, 3]
ready
code 1
function findDuplicatesNestedLoop(arr) {
  const duplicates = [];
  
  for (let i = 0; i < arr.length; i++) {
    for (let j = i + 1; j < arr.length; j++) {
      if (arr[i] === arr[j] && !duplicates.includes(arr[i])) {
        duplicates.push(arr[i]);
      }
    }
  }
  
  return duplicates;
}

const myArray = [1, 2, 3, 2, 4, 3];
console.log(findDuplicatesNestedLoop(myArray)); // Output: [2, 3]
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.