# Spot the bug - #60

**URL:** <https://forum.kirupa.com/t/spot-the-bug-60/682167>\
**Category:** web dev\
**Created:** [June 19, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-60/682167 "2026-06-19T07:00:07Z")\
**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 19, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-60/682167/1 "2026-06-19T07:00:07Z")

</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:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [June 20, 2026, 7:20am UTC](https://forum.kirupa.com/t/spot-the-bug-60/682167/2 "2026-06-20T07:20:27Z")

</div>

The return values are flipped. Right now you return `false` when you _find_ a duplicate, and `true` only when you _don’t_ find any—so `hasDuplicate([2, 7, 4, 7])` prints `false` even though it clearly has one. The fix is just swapping the booleans:

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

```

Mechanism-wise: the loop is correctly detecting “we’ve seen this before, ” but the function’s contract (“has duplicate? ”) needs to treat that condition as success, not failure. I found a related kirupa. com article that can help you go deeper into this topic.
