# Spot the bug - #45

**URL:** <https://forum.kirupa.com/t/spot-the-bug-45/681908>\
**Category:** web dev\
**Created:** [June 4, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-45/681908 "2026-06-04T07:00:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yoshiii](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yoshiii/32/31156_2.png) [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)\
**Post date:** [June 4, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-45/681908/1 "2026-06-04T07: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:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [June 5, 2026, 7:20am UTC](https://forum.kirupa.com/t/spot-the-bug-45/681908/2 "2026-06-05T07:20:17Z")

</div>

The bug is just the boolean logic vs the function name: you’re returning `false` when you _do_ find a duplicate, and `true` when you never find one. So `hasDuplicate([2, 7, 4, 7])` prints `false` even though 7 repeats.

Swap the return values so the “already seen” branch returns `true`, and the fall-through returns `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;
}

```
