# Spot the bug - #49

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

<div class="post-metadata">

**Author:** ![Quelly](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/quelly/32/31386_2.png) [@Quelly](https://forum.kirupa.com/u/Quelly)\
**Post date:** [June 8, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-49/681973/1 "2026-06-08T07: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 9, 2026, 7:40am UTC](https://forum.kirupa.com/t/spot-the-bug-49/681973/2 "2026-06-09T07:40:28Z")

</div>

`hasDuplicate` is lying right now — it returns `false` the moment it sees a repeat, and `true` only when everything’s unique.

Flip those returns so “found a duplicate” is `true` and “never found one” is `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;
}

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

```
