Count Occurrence of an Element in an Array

Count Occurrence of an Element in an Array” is a basic problem of array data structure. Here, we are given an array of size ‘n’ and an element ‘x’. Our task is to count the occurrences of an element ‘x’ in an array.


Enter the size of an array: 10
Enter Array Elements:
12 13 11 12 45 67 13 11 12 43
Enter an element to count: 12
The number of occurrences of 12 are: 3

The steps required to count occurrences of an element are as follows:

  1. Scan the size of array and elements of the array from user.
  2. Scan the key element whose occurrences needed to be count from the array.
  3. Set Count = 0.
  4. Iterate over each element and check whether it is equal to key element or not. If element is key element, increment count as: Count = Count + 1.
  5. Print the result as Count.

C++ Program to count occurrences of an element is as follows:

/* Program to count occurence of key element in an array */  
using namespace std;  
int main()  
    /* Scan size of array */  
    int n;  
    cout<<"Enter the size of an array: ";  
    /* Scan Array Elements */  
    int arr[n];  
    cout<<"\nEnter array elements:\n";  
    for(int i = 0; i < n; i++)  
    /* Scan key element */  
    int key;  
    cout<<"\nEnter key element: ";  
    int count = 0;  
    /* Count occurence of key element in an array */  
    for(int i = 0; i < n; i++)  
        if(arr[i] == key)  
        count = count + 1;  
    /*Printing the result */  
    cout<<"\nThe number of occurences of "<<key<<" are: "<<count;  

Enter the size of an array: 
Enter array elements:
1 3 2 3 5
Enter key element: 
The number of occurences of 3 are: 2
Time Complexity:
O(n), where ‘n’ is the size of an element

Related Posts:

  1. Program to search an element in an Array (Linear Search).
  2. Program to Rotate Array Elements by ‘D’ positions.
  3. Program to find most frequent element in an array.
  4. Program to find pair in an array with given sum.
  5. Program to find first repeating element of an array.
  6. Program to merge two sorted arrays.
  7. Program to find missing number in an array.
  8. Program to sort if array is sorted.
  9. Program to print Alternate Elements of an Array.
  10. Program to swap kth element from beginning to kth element from end in an Array.
  11. Program to print all possible subarrays of the given array.
  12. Program to print kth smallest and kth largest element of an Array.
  13. Program to find equilibrium index of an Array.
  14. Program to find majority element of an Array.
  15. Program to find mean of the Array.
  16. Program to sort an Array of 0s and 1s.
  17. Program to Reverse an Array.
  18. Program to count number of odd numbers and even numbers in an array.
  19. Program to find absolute difference between sum of odd index elements and even index elements.
  20. Program to find smallest and largest element of the array.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *