C Program To Count Occurrence of Array Element
Learn How To Count Occurrence of Array Element in C Programming Language. It is important that we should know How A For Loop Works before getting further with the C Program Code. This program calculates the Total Number of Times an Element occurs within a 1-Dimensional Array in C Code. The Frequency of the Element is checked by using the Count variable.
Also Read: Free 100+ C Programs For Strings, Arrays, Numbers and much more
C Program To Count Occurrence of Array Element
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include<stdio.h> int main() { int array[20], limit, count, search, incr = 0; printf("Enter Array Limit:\t"); scanf("%d", &limit); printf("Enter the Elements into the Array\n"); for(count = 0; count < limit; count++) { scanf("%d", &array[count]); } printf("The Elements of the Array\n"); for(count = 0; count < limit; count++) { printf("%d\t", array[count]); } printf("\nEnter the Element to Search:\t"); scanf("%d", &search); for(count = 0; count < 5; count++) { if(array[count] == search) { incr++; } } if(incr == 0) { printf("\nElement Not found\n"); } printf("Element %d exists %d time(s) in the array\n", search, incr); printf("\n"); return 0; } |
Also Read: 50+ Data Structures Programs in C Programming
Output

If you have any compilation errors or doubts in this C Program To Count Occurrence of Array Element, let us know about it in the Comment Section below.
You have mentioned array size as 20. What if the end user wants to enter elements more than 20?
You can do two things:
1. Take Array Limit as 100 or 200 and then accept the Array Limit from the user. Execute the For Loop till the User’s Limit.
2. Use Dynamic Memory Allocation technique by using malloc() which is more memory efficient.
Excellent. So, this is basically comparing the search element with every element of the array and then incrementing a counter variable whenever the same element is found.