我正在尝试解决来自Codewars的挑战,您必须在数组中找到出现奇数次的数字。我修改了答案以返回出现奇数次且最频繁的数字。但它总是导致 0
counts={};
function findOdd(A) {
for (var i=0; i<A.length; i++){
if ((typeof counts["a" + toString(A[i])]) !== 'undefined'){
counts["a" + toString(A[i])]++;
}
else{
counts["a" + toString(A[i])]=1;
}
}
max = 0;
for (a in counts){
if (counts[a]>max && counts[a]%2!==0){
max = counts[a]
}
}
return max;
}
var testArray=[];
for (var i =0; i<100; i++){
testArray.push(Math.ceil(Math.random()*100))
}
console.log(findOdd(testArray));
郎朗坤
相关分类