# Spot the bug - #58

**URL:** <https://forum.kirupa.com/t/spot-the-bug-58/682128>\
**Category:** web dev\
**Created:** [June 17, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-58/682128 "2026-06-17T07:00:09Z")\
**Posts on this page:** 2\
**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 17, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-58/682128/1 "2026-06-17T07: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:** ![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 18, 2026, 7:20am UTC](https://forum.kirupa.com/t/spot-the-bug-58/682128/2 "2026-06-18T07:20:30Z")

</div>

this function is doing the “mission failed successfully” thing: it returns `false` right when it finds a duplicate.

`seen.has(n)` means “we’ve already seen n”, so that branch should return `true`, and only after the loop should it return `false` if nothing repeated.
