Code Logo

Even Count

Published at10 Jan 2026
Easy 3 views
Like1

This problem is very direct: count how many numbers in the array are even. A number is even if dividing it by 2 leaves no remainder.

You are not summing the even numbers or returning their positions. The only thing you need is the total count of values that satisfy the even-number rule.

For example, if nums = [1,2,3,4,5,6], the answer is 3 because 2, 4, and 6 are even. If nums = [1,3,5], the answer is 0 because none of the numbers are even. If every number is even, then the answer is just the array length.

So the task is to scan the array and count how many elements satisfy num % 2 === 0.

Example Input & Output

Example 1
Input
nums = [1, 2, 3, 4, 5, 6]
Output
3
Explanation

Example 1: There are 3 even numbers (2, 4, 6)

Example 2
Input
nums = [1, 3, 5]
Output
0
Explanation

Example 2: No even numbers in [1, 3, 5]

Example 3
Input
nums = [2, 4, 6, 8]
Output
4
Explanation

Example 3: All 4 numbers are even

Algorithm Flow

Recommendation Algorithm Flow for Even Count
Recommendation Algorithm Flow for Even Count

Solution Approach

The simplest approach is a single pass through the array with a counter. Every time we see an even number, we add 1 to that counter.

We start from zero:

let count = 0;

Then check each number:

for (const num of nums) {
  if (num % 2 === 0) {
    count++;
  }
}

The condition num % 2 === 0 means the number divides evenly by 2, so it belongs in the count.

At the end, return the total:

return count;

This runs in O(n) time and uses O(1) extra space.

Best Answers

java
class Solution {
    public int count_even(int[] nums) {
        int count = 0;
        for (int num : nums) {
            if (num % 2 == 0) count++;
        }
        return count;
    }
}