# First Duplicate Number

![](https://3743232000-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHW2IQuh2PFpWJDvBz2FF%2Fuploads%2Fgit-blob-acb7a4e40299b87f22770a001dcbd238f49d5e95%2FScreenshot%202023-01-21%20at%2020.45.41.png?alt=media)

* 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;
```
