# Spot the bug - #176: Duplicate Finder Helper

**URL:** <https://forum.kirupa.com/t/spot-the-bug-176-duplicate-finder-helper/683751>\
**Category:** web dev\
**Created:** [October 16, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-176-duplicate-finder-helper/683751 "2026-10-16T07:00:07Z")\
**Posts on this page:** 3\
**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:** [October 16, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-176-duplicate-finder-helper/683751/1 "2026-10-16T07: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:** ![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:** [October 16, 2026, 7:04am UTC](https://forum.kirupa.com/t/spot-the-bug-176-duplicate-finder-helper/683751/2 "2026-10-16T07:04:33Z")

</div>

The return values are reversed. The function returns false when it finds a duplicate and true when there are no duplicates. Fix it by returning true inside the seen.has(n) condition and false at the end. For [2, 7, 4, 7], it should return true.

---

<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:** [October 16, 2026, 7:04am UTC](https://forum.kirupa.com/t/spot-the-bug-176-duplicate-finder-helper/683751/3 "2026-10-16T07:04:44Z")

</div>

I see what you mean.

That tradeoff shows up a lot once people start using the thing daily.
