Taken from Execute Program
Write a
hasDuplicates
function. It should use a set to decide whether an array of numbers has any duplicates. It should returntrue
if there are duplicates; otherwise it should returnfalse
.
Solution
This is the answer I came up with:
function hasDuplicates(numbers: Array<any>) {
let tempSet = new Set()
for (let number of numbers) {
if (tempSet.has(number)) {
return true
}
tempSet.add(number)
}
return false
}