javascript function that return the odd numbers in array code example

Example 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

Example 2: get odd number in array

public int[] findEvens() {
  int numberEvens = 0;
  for (int i = 0; i < numbers.length; i++) {
     if (i % 2 == 0) {
        numberEvens++;
     }
  }

Tags:

Misc Example