Currently, I am tackling a challenge on Codewars that requires identifying the element in an array that occurs an odd number of times. The current solution I have successfully passes 3 out of 6 tests.
function findOdd(A) {
//happy coding!
let odd = "";
let count = 0;
for (let i = 0; i < A.length; i++){
for (let j = 0; j < A.length; j++){
if (A[i] === A[j] ){
count++;
}
if (count %2 != 0){
odd = A[i];
}
}
}
return odd;
}
The following are the test cases that did not pass:
A1: [ 1, 1, 2, -2, 5, 2, 4, 4, -1, -2, 5 ]
Expected result: -1
A2: [ 1, 1, 1, 1, 1, 1, 10, 1, 1, 1, 1 ]
Expected result: 10
A3: [ 5, 4, 3, 2, 1, 5, 4, 3, 2, 10, 10 ]
Expected result: 1
Meanwhile, the following are the test cases that succeeded:
A4: [ 20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5 ]
Expected result: 5
A5: [ 20, 1, 1, 2, 2, 3, 3, 5, 5, 4, 20, 4, 5 ]
Expected result: 5
A6: [ 1, 1, 1, 1, 1, 1, 10, 1, 1, 1, 1 ]
Expected result: 10
Here, A1 - A6 represent the input arrays and n1 - n6 denote the expected outcomes.