# Spot the bug - #59

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

<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 18, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-59/682145/1 "2026-06-18T07:00:09Z")

</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:** ![Ellen1979](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ellen1979/32/31260_2.png) [@Ellen1979](https://forum.kirupa.com/u/Ellen1979)\
**Post date:** [June 19, 2026, 7:20am UTC](https://forum.kirupa.com/t/spot-the-bug-59/682145/2 "2026-06-19T07:20:24Z")

</div>

Your `return` values are backwards. Hitting `seen.has(n)` means you found a duplicate, so `hasDuplicate` should return `true` there, and only return `false` after the loop finishes with no repeats.

I’d flip the returns:

```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

```
