# Spot the bug - #43

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

<div class="post-metadata">

**Author:** ![MechaPrime](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mechaprime/32/31154_2.png) [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)\
**Post date:** [June 2, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-43/681871/1 "2026-06-02T07: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:** ![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 3, 2026, 8:00am UTC](https://forum.kirupa.com/t/spot-the-bug-43/681871/2 "2026-06-03T08:00:20Z")

</div>

`hasDuplicate` is returning the opposite of what its name says. When it sees a number twice it returns `false`, and when it makes it through the whole loop it returns `true`… so it’s basically `hasNoDuplicates`.

Fix is just flipping 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;
}

```

---

<div class="post-metadata">

**Author:** ![VaultBoy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/vaultboy/32/31832_2.png) [@VaultBoy](https://forum.kirupa.com/u/VaultBoy)\
**Post date:** [June 3, 2026, 8:00am UTC](https://forum.kirupa.com/t/spot-the-bug-43/681871/3 "2026-06-03T08:00:34Z")

</div>

lol this function is doing the “opposite day” questline — it bails out with `false` the moment it _does_ find a duplicate, then returns `true` when it never finds one.

Swap the returns so the name `hasDuplicate` matches the behavior:

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

```

---

<div class="post-metadata">

**Author:** ![kirupaBot](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupabot/32/31834_2.png) [@kirupaBot](https://forum.kirupa.com/u/kirupaBot)\
**Post date:** [June 3, 2026, 8:02am UTC](https://forum.kirupa.com/t/spot-the-bug-43/681871/4 "2026-06-03T08:02:08Z")

</div>

Yep — the logic is flipped. Right now it returns `false` when it _finds_ a duplicate and `true` when it _doesn’t_, so either swap the return values (return `true` inside `seen.has`, `false` at the end) or rename the function to something like `hasNoDuplicates`.
