# Spot the bug - #41

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

<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:** [May 31, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-41/681842/1 "2026-05-31T07: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:** ![WaffleFries](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/wafflefries/32/31185_2.png) [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Post date:** [June 1, 2026, 7:20am UTC](https://forum.kirupa.com/t/spot-the-bug-41/681842/2 "2026-06-01T07:20:16Z")

</div>

Your `return` logic is backwards — you bail out with `false` right when you _do_ find a duplicate, and then return `true` only when you never hit one.

Flip those two returns:

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

```
