我正在嘗試解決來自Codewars的挑戰,您必須在數組中找到出現奇數次的數字。我修改了答案以返回出現奇數次且最頻繁的數字。但它總是導致 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:函數返回 0 而不是出現奇數次的數字
RISEBY
2022-10-21 11:00:18
