我正在嘗試解決來自Codewars的挑戰(zhàn),您必須在數(shù)組中找到出現(xiàn)奇數(shù)次的數(shù)字。我修改了答案以返回出現(xiàn)奇數(shù)次且最頻繁的數(shù)字。但它總是導(dǎo)致 0counts={};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));
Javascript:函數(shù)返回 0 而不是出現(xiàn)奇數(shù)次的數(shù)字
RISEBY
2022-10-21 11:00:18