Explore Library
Code QuizAdvanced

Duplicate Detection With a Set

Spot the ordering bug when using a hash set to detect duplicate values in an array.

Codejavascript
function hasDuplicate(arr) {
  const seen = new Set();
  for (const num of arr) {
    seen.add(num);
    if (seen.has(num)) {
      return true;
    }
  }
  return false;
}

console.log(hasDuplicate([1, 2, 3])); // expected false

What is the bug in this code?