Counting Matching Elements

Counting matching elements in an array means looking at each item one by one and keeping a running tally of how many times a specific target value appears. Imagine you are a teacher checking how many students scored exactly 50 marks in a math test by going down the roll list and dropping a pebble in

The ideaLink to this section

Counting matching elements in an array means looking at each item one by one and keeping a running tally of how many times a specific target value appears. Imagine you are a teacher checking how many students scored exactly 50 marks in a math test by going down the roll list and dropping a pebble in a jar each time you see a 50. In Java, we loop through the array, check each element against our condition using an if statement, and add 1 to a counter variable whenever it matches. By the end of the loop, our counter holds the exact frequency of the target value. This is a foundational array technique used everywhere from simple data filtering to basic statistics.

note

When to use it: Use this idea whenever a problem asks you to find the frequency, occurrence count, or number of elements in an array that satisfy a specific condition.

Worked example: Counting Winning ScoresLink to this section

A cricket coach wants to know how many players in a team scored exactly 50 runs in the recent tournament. Given an array of runs scored by each player, count how many players scored 50.

Input format

  • Line 1: an integer N, the number of players
  • Line 2: N space-separated integers, the runs scored by each player

Output format

  • Line 1: a single integer representing the count of players who scored 50

How it worksLink to this section

  1. Read N = 5 and create an array of size 5.
  2. Read the 5 run values: 25, 50, 75, 50, and 10 into the array.
  3. Initialize the counter variable to 0.
  4. Loop through the array: index 0 is 25 (no match), index 1 is 50 (count becomes 1), index 2 is 75 (no match), index 3 is 50 (count becomes 2), index 4 is 10 (no match).
  5. Print the final count of 2.

note

Time complexity: O(n). We traverse the array of size n once to read the inputs and once to count the matching elements. Space complexity: O(n). We store all n elements in an array.

warning

A common beginner mistake is forgetting to initialize the counter variable to zero outside the loop, or declaring inside the loop so it resets on every iteration.

Quick checkLink to this section

Counting Matching Elements quiz
Question 1 of 2

What is the primary purpose of a counter variable when traversing an array?

Practice 1: Count Defective Bulbs (Medium)Link to this section

tip

Hint: Loop through the array and check if each element is equal to 0, incrementing a counter each time.

Practice 2: Passing Students Count (Hard)Link to this section

tip

Hint: Iterate through the array and check if the mark is greater than or equal to 40 before increasing your count.

Rate this content

Sign in to rate

Discussion

Sign in to join the discussion