# Spot the bug - #84

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

<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:** [July 12, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-84/682580/1 "2026-07-12T07:00:08Z")

</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:** ![ArthurDent](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/arthurdent/32/31262_2.png) [@ArthurDent](https://forum.kirupa.com/u/ArthurDent)\
**Post date:** [July 14, 2026, 12:40am UTC](https://forum.kirupa.com/t/spot-the-bug-84/682580/2 "2026-07-14T00:40:25Z")

</div>

Your return values are flipped relative to the function name/intent. Right now it returns `false` when it finds a duplicate, and `true` when it gets through the loop without finding one. Fix it by returning `true` on the duplicate branch and `false` at the end:

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

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

```

Confidence: high.
