How to find find the odd numbers in an array

Best way to find odd number in array is Array filter() method, passing a callback that returns true when the number is odd, and false otherwise on the ase of condistion.

 


const array= [1, 2, 3 ,4, 5, 6, 7, 8, 9,10];
const odds = array.filter((num) => num % 2 === 1);
console.log(odds);

 

Here is another way to find odd numbers in an array 

 



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

Tags:

Share:

Related posts