Answers for "check if number appears odd number of times in array javascript"

1

js array value that appears odd number of times

// Find the odd int
function findOdd(A) {
  return A.reduce((a, b) => a ^ b);
}

console.log(findOdd([20,1,-1,2,-2,3,3,5,5,1,2,4,20,4,-1,-2,5], 5)); // 5
console.log(findOdd([1,1,2,-2,5,2,4,4,-1,-2,5], -1)); // -1
console.log(findOdd([20,1,1,2,2,3,3,5,5,4,20,4,5], 5)); // 5
Posted by: Guest on March-18-2021
0

check if number appears odd number of times in array javascript

function findOdd(A) {
    let counts = A.reduce((p, n) => (p[n] = ++p[n] || 1, p), {});
    return +Object.keys(counts).find(k => counts[k] % 2) || undefined;
}
Posted by: Guest on June-16-2020

Code answers related to "check if number appears odd number of times in array javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language