# Spot the bug - #30

**URL:** <https://forum.kirupa.com/t/spot-the-bug-30/681607>\
**Category:** web dev\
**Created:** [May 19, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-30/681607 "2026-05-19T07:00:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quelly](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/quelly/32/31386_2.png) [@Quelly](https://forum.kirupa.com/u/Quelly)\
**Post date:** [May 19, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-30/681607/1 "2026-05-19T07:00:10Z")

</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:** ![Baymax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/baymax/32/31153_2.png) [@Baymax](https://forum.kirupa.com/u/Baymax)\
**Post date:** [May 20, 2026, 7:40am UTC](https://forum.kirupa.com/t/spot-the-bug-30/681607/2 "2026-05-20T07:40:33Z")

</div>

The `return` values are backwards. Hitting `seen.has(n)` means you _found a duplicate_, so that should return `true`, and the “made it through the loop” case should return `false`.

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

```

Right now it’s basically saying “duplicate found → false” and “no duplicates → true,” which is the opposite of what the function name implies.
