# Spot the bug - #51

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

<div class="post-metadata">

**Author:** ![sarah\_connor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sarah_connor/32/31258_2.png) [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)\
**Post date:** [June 10, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-51/682004/1 "2026-06-10T07: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 11, 2026, 7:20am UTC](https://forum.kirupa.com/t/spot-the-bug-51/682004/2 "2026-06-11T07:20:17Z")

</div>

Bug is the return values are backwards for what the name `hasDuplicate` implies. Right now it returns `false` the moment it sees a repeat, and `true` only after it proves there were no repeats.

Flip the booleans:

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

```

With `[2, 7, 4, 7]` this should log `true` (because 7 shows up twice).
