Counting the Number of Even and Odd Elements in an Array in Java

Understanding Even and Odd Count

This task involves counting the number of even and odd elements in an array.

We will explore three different methods to achieve this in Java.

Method 1: Using Loop

This method iterates through the array and counts even and odd elements.

public class EvenOddCount {
    public static void countEvenOdd(int[] arr) {
        int evenCount = 0, oddCount = 0;
        for (int num : arr) {
            if (num % 2 == 0)
                evenCount++;
            else
                oddCount++;
        }
        System.out.println("Even Count: " + evenCount + ", Odd Count: " + oddCount);
    }
    
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
        countEvenOdd(arr);
    }
}
            
Output:
Even Count: 4, Odd Count: 4

Method 2: Using Recursion

This method counts even and odd numbers using recursion.

public class EvenOddRecursion {
    public static void countEvenOddRec(int[] arr, int index, int evenCount, int oddCount) {
        if (index == arr.length) {
            System.out.println("Even Count: " + evenCount + ", Odd Count: " + oddCount);
            return;
        }
        if (arr[index] % 2 == 0)
            evenCount++;
        else
            oddCount++;
        countEvenOddRec(arr, index + 1, evenCount, oddCount);
    }
    
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
        countEvenOddRec(arr, 0, 0, 0);
    }
}
            
Output:
Even Count: 4, Odd Count: 4

Method 3: Using Streams

This method uses Java Streams to determine even and odd numbers.

import java.util.Arrays;

public class EvenOddStream {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
        long evenCount = Arrays.stream(arr).filter(num -> num % 2 == 0).count();
        long oddCount = arr.length - evenCount;
        System.out.println("Even Count: " + evenCount + ", Odd Count: " + oddCount);
    }
}
            
Output:
Even Count: 4, Odd Count: 4
Top 100 Codes By Learn-for-free
Start Preparing Arraysform here👇

Below You will find some of the most important codes in languages like C, C++, Java, and Python. These codes are of prime importance for college semester exams and online tests.

Getting Started

Find Largest Element in an Array: C C++ Java Python

Find Smallest Element in an Array: C C++ Java Python

Find the Smallest and Largest Element in an Array: C C++ Java Python

Find Second Smallest Element in an Array: C C++ Java Python

Calculate the Sum of Elements in an Array: C C++ Java Python

Reverse an Array: C C++ Java Python

Sort First Half in Ascending Order and Second Half in Descending: C C++ Java Python

Finding the Frequency of Elements in an Array: C C++ Java Python

Counting the Number of Even and Odd Elements in an Array: C C++ Java Python

Finding Maximum Product Sub-array in a Given Array: C C++ Java Python

Finding Arrays are Disjoint or Not: C C++ Java Python

Finding Equilibrium Index of an Array: C C++ Java Python

Rotation of Elements of Array - Left and Right: C C++ Java Python

Balanced Parenthesis Problem: C C++ Java Python