First Duplicate Number

  • N, N

function firstDuplicateValue(arr) {
  const memo = new Set();

  for (let i = 0; i < arr.length; i++) {
    const num = arr[i];
    if (memo.has(num)) return arr[i];
    memo.add(num);
  }
  
  return -1;
}

// Do not edit the line below.
exports.firstDuplicateValue = firstDuplicateValue;
  • N, 1

Last updated