Find Duplicate

Find the first duplicate element that appears in an array using a hash set for efficient lookup.

Code

Algorithms
const seen = new Set();
let duplicate = undefined;
for (const x of arr) {
  if (seen.has(x)) { duplicate = x; break; }
  seen.add(x);
}
return duplicate;

Parameters

Array to check

Browser·fetch() may be limited by CORS

More JavaScript Snippets