Question 3
Question
Answer
function hasDuplicates(arr) {
const seen = new Set();
for (const item of arr) {
if (seen.has(item)) {
return true; // Duplicate found!
}
seen.add(item);
}
return false; // No duplicates found
}
// Example usage:
const array1 = [1, 2, 3, 4, 5];
console.log(hasDuplicates(array1)); // Output: false
const array2 = [1, 2, 3, 2, 4];
console.log(hasDuplicates(array2)); // Output: trueLast updated