Find First Duplicate Value in Large Array

function findDuplicate(arr) {
  const a = new Set();
  for (const n of arr) {
    if (a.has(n)) return n;
    else a.add(n);
  }
  return -1;
}