> For the complete documentation index, see [llms.txt](https://algorithm.prettylog.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://algorithm.prettylog.com/algorithm-problems/algoexpert/medium/first-duplicate-number.md).

# First Duplicate Number

![](/files/RGILjKowoWedOlOhMHAa)

* N, N

```jsx
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

```jsx
function firstDuplicateValue(arr) {

  for (let i = 0; i < arr.length; i++) {
    const num = Math.abs(arr[i]);
    if (arr[num - 1] < 0) return num;
    arr[num - 1] = arr[num - 1] * -1;
  }
  
  return -1;
}

// Do not edit the line below.
exports.firstDuplicateValue = firstDuplicateValue;
```
