Spot the bug - #176: Duplicate Finder Helper

There is one subtle logic bug.

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.

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.

I see what you mean.

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