C Program to count how many times a given number occurs in an array
Learn how to count the occurrences of a given number in a C array using one traversal, with validated code, examples, a dry run, and complexity analysis.
To count how many times a given number occurs in an array, compare the target with every element and increase a counter whenever the two values match.
For example, the number 4 occurs four times in this array:
4 7 4 2 4 9 4
Unlike a search that stops after the first match, an occurrence-counting program must examine the complete array so that it does not miss later matches.
C Program to Count the Occurrences of a Given Number
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int array[MAX_SIZE];
int size;
int target;
int occurrenceCount = 0;
printf("Enter the number of elements: ");
if (scanf("%d", &size) != 1 || size < 1 || size > MAX_SIZE) {
printf("Please enter a size between 1 and %d.\n", MAX_SIZE);
return 1;
}
printf("Enter %d elements:\n", size);
for (int index = 0; index < size; index++) {
if (scanf("%d", &array[index]) != 1) {
printf("Invalid array element.\n");
return 1;
}
}
printf("Enter the number to count: ");
if (scanf("%d", &target) != 1) {
printf("Invalid number.\n");
return 1;
}
for (int index = 0; index < size; index++) {
if (array[index] == target) {
occurrenceCount++;
}
}
printf("%d occurs %d time%s in the array.\n",
target,
occurrenceCount,
occurrenceCount == 1 ? "" : "s");
return 0;
}
Sample Output
Enter the number of elements: 7
Enter 7 elements:
4 7 4 2 4 9 4
Enter the number to count: 4
4 occurs 4 times in the array.
The conditional expression in the final printf() uses time when the count is exactly 1 and times for every other count.
How the Program Works
occurrenceCountis initialized to0before the search begins.- The program reads the array and the target number.
- A loop visits every valid array index from
0throughsize - 1. - If
array[index] == target, the counter increases by one. - The loop does not use
breakbecause the target may occur again later. - After the final element is checked, the counter contains the target's frequency.
Here is a dry run for target 4:
| Index | Element | Match? | Occurrence count |
|---|---|---|---|
| 0 | 4 | Yes | 1 |
| 1 | 7 | No | 1 |
| 2 | 4 | Yes | 2 |
| 3 | 2 | No | 2 |
| 4 | 4 | Yes | 3 |
| 5 | 9 | No | 3 |
| 6 | 4 | Yes | 4 |
The array is only read during this process, so its values and ordering remain unchanged.
What If the Number Does Not Occur?
If no element equals the target, the counter remains 0:
Enter the number of elements: 5
Enter 5 elements:
3 8 1 6 10
Enter the number to count: 7
7 occurs 0 times in the array.
A count of zero clearly indicates that the target does not exist in the array.
Does It Work with Negative Numbers and Zero?
Yes. Equality comparison works the same way for positive numbers, negative numbers, and zero. For example, -3 occurs three times in -3, 5, 0, -3, -3.
No special condition is required because the program compares integer values directly.
Why Not Stop at the First Match?
Using break after the first match is correct when the task only asks whether a number exists or requests its first index. It is incorrect when counting occurrences because later elements may contain additional matches.
For 4, 7, 4, 2, 4, stopping at index 0 would report one occurrence even though the correct count is three.
Counting One Number Versus Every Element's Frequency
This program receives one target and counts only that value in O(n) time. A program that prints the frequency of every distinct element solves a broader problem and usually needs additional storage or nested comparisons.
Use this approach when the question asks about one specified number rather than the frequency distribution of the entire array.
Can Counting Be Done While Reading the Array?
The target must be known before the elements are read to count matches during input. If the target is entered first, the program can compare and count each value immediately after scanf() stores it.
The complete program reads the array first so that it can ask for the target afterward and preserve the values for other operations. Both approaches have O(n) time complexity.
Time and Space Complexity
- Time complexity:
O(n)in every case because allnelements must be checked to obtain the complete count. - Extra space complexity:
O(1)for counting because only the target, counter, and loop index are needed. The input array itself occupiesO(n)space.
Common Mistakes
- Forgetting to initialize
occurrenceCountto0. - Stopping at the first match with
break. - Using
=instead of==in the comparison. - Increasing the counter for nonmatching elements.
- Looping with
index <= size, which reads beyond the last valid index. - Reporting “not found” when the count is
1due to an incorrect condition.
By checking every element and incrementing one counter for each match, the program finds the exact number of occurrences in a single traversal.