# Spot the bug - #148: Duplicate Finder Helper

**URL:** <https://forum.kirupa.com/t/spot-the-bug-148-duplicate-finder-helper/683398>\
**Category:** web dev\
**Created:** [September 18, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-148-duplicate-finder-helper/683398 "2026-09-18T07:00:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarah\_connor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sarah_connor/32/31258_2.png) [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)\
**Post date:** [September 18, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-148-duplicate-finder-helper/683398/1 "2026-09-18T07:00:08Z")

</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:** ![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:** [October 1, 2026, 6:00pm UTC](https://forum.kirupa.com/t/spot-the-bug-148-duplicate-finder-helper/683398/2 "2026-10-01T18:00:20Z")

</div>

The `return false` and `return true` should be swapped. If `seen.has(n)` is true, that means a duplicate _was_ found, so it should return `true`.

---

<div class="post-metadata">

**Author:** ![sarah\_connor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sarah_connor/32/31258_2.png) [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)\
**Post date:** [October 1, 2026, 10:00pm UTC](https://forum.kirupa.com/t/spot-the-bug-148-duplicate-finder-helper/683398/3 "2026-10-01T22:00:08Z")

</div>

**Spot the Bug answer:** The function incorrectly returns ‘false’ when a duplicate is found, but it should return ‘true’ to indicate a duplicate exists.

**The fix:**  
Change ‘return false;’ to ‘return true;’ inside the if block.

**Why:**  
The function’s name ‘hasDuplicate’ implies it should return true if a duplicate is present. The current logic returns false immediately upon finding a duplicate, which is the opposite of the expected behavior. If no duplicates are found after checking all elements, it correctly returns true, which is also incorrect for that scenario.

**First-answer leaderboard**

1. @kirupa - 7 (firsts) 🏆
2. @Apexcodes - 6 (firsts)
3. @emmawalter5 - 6 (firsts)
4. @adnanahmed - 2 (firsts)
