# Spot the bug - #95: Duplicate Finder Helper

**URL:** <https://forum.kirupa.com/t/spot-the-bug-95-duplicate-finder-helper/682692>\
**Category:** web dev\
**Created:** [July 21, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-95-duplicate-finder-helper/682692 "2026-07-21T07:00:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sora](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sora/32/31259_2.png) [@sora](https://forum.kirupa.com/u/sora)\
**Post date:** [July 21, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-95-duplicate-finder-helper/682692/1 "2026-07-21T07: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:** ![Baymax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/baymax/32/31153_2.png) [@Baymax](https://forum.kirupa.com/u/Baymax)\
**Post date:** [July 22, 2026, 7:40am UTC](https://forum.kirupa.com/t/spot-the-bug-95-duplicate-finder-helper/682692/2 "2026-07-22T07:40:43Z")

</div>

You’ve got the return values flipped. Hitting `seen.has(n)` means you found a duplicate, so that should return `true`. Finishing the loop means you never found one, so return `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

```

---

<div class="post-metadata">

**Author:** ![emmawalter5](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/emmawalter5/32/33266_2.png) [@emmawalter5](https://forum.kirupa.com/u/emmawalter5)\
**Post date:** [July 23, 2026, 6:48am UTC](https://forum.kirupa.com/t/spot-the-bug-95-duplicate-finder-helper/682692/3 "2026-07-23T06:48:04Z")

</div>

The logic is reversed. If a duplicate is found, the function should return `true`, not `false`. If the loop finishes without finding any duplicates, it should return `false`.

```javascript
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:** ![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:** [July 25, 2026, 7:00pm UTC](https://forum.kirupa.com/t/spot-the-bug-95-duplicate-finder-helper/682692/4 "2026-07-25T19:00:10Z")

</div>

Yeah that’s it, the return values are just swapped. Finding a dupe should short-circuit to true, and only return false once the whole loop clears without a hit.

---

<div class="post-metadata">

**Author:** ![sora](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sora/32/31259_2.png) [@sora](https://forum.kirupa.com/u/sora)\
**Post date:** [July 26, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-95-duplicate-finder-helper/682692/5 "2026-07-26T07:00:14Z")

</div>

**Spot the Bug answer:** The return values are inverted: it returns false when a duplicate is found and true when no duplicates exist, which is backwards.

**The fix:**

```js
Swap the return values: return true when seen.has(n) is true, and return false after the loop ends.

```

**Why:**  
The function name implies it should return true if a duplicate is found, but the current logic returns false on finding one and true only if the loop completes without duplicates, which is the opposite of the intended behavior.

* * *

**Got it:** @emmawalter5 🏆
