I'm getting an error and have idea why 'count' is returning an error. Please help.
Write a function called findTwins, which accepts an array of integers and finds two of same numbers and returns the number that is repeated twice. The function should return null if there is not a number repeated twice.
function findTwins(arr) {
if (arr.length === 0) return null;
let count = {};
//count occurances of each item
for (let i = 0; i < arr.length; i++) {
let num = arr[i];
//if element isn't in given key, assign it value '0'
if (count[num] === undefined) {
count[num] = 0;
} //if element has occured in object, increase it by 1
count[num]++;
}
//iterate through count object
for (let key in count) {
//if value of count is 2, returns that key
if (count[key] === 2) {
return key;
//if there are no repeats in array, return null
} else {
(count[key] === 0) {
return null;
}
}
}
}
console.log(
findTwins([2, 3, 6, 34, 7, 8, 2]), // 2
findTwins([]), // null
findTwins([3, 1, 4, 2, 5]) // null
)