# Spot the bug - #74

**URL:** <https://forum.kirupa.com/t/spot-the-bug-74/682419>\
**Category:** web dev\
**Created:** [July 3, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-74/682419 "2026-07-03T07:00:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![VaultBoy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/vaultboy/32/31832_2.png) [@VaultBoy](https://forum.kirupa.com/u/VaultBoy)\
**Post date:** [July 3, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-74/682419/1 "2026-07-03T07:00:09Z")

</div>

There is one subtle logic bug.

```js
function hasDuplicate(nums) {
  const seen = new Set();
  for (const n of nums) {
    if (seen.has(n)) {
      return false;
    }
    seen.add(n);
  }
  return true;
}

console.log(hasDuplicate([2, 7, 4, 7]));

```

Reply with what is broken and how you would fix it.

---

<div class="post-metadata">

**Author:** ![Ellen1979](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ellen1979/32/31260_2.png) [@Ellen1979](https://forum.kirupa.com/u/Ellen1979)\
**Post date:** [July 4, 2026, 7:20am UTC](https://forum.kirupa.com/t/spot-the-bug-74/682419/2 "2026-07-04T07:20:28Z")

</div>

Your returns are flipped for what the name `hasDuplicate` promises: you hit a repeat and return `false`, then you fall through and return `true` when everything was unique.

Fix is just swapping those two returns:

```auto
function hasDuplicate(nums) {
  const seen = new Set();
  for (const n of nums) {
    if (seen.has(n)) return true;
    seen.add(n);
  }
  return false;
}

```
