Count All Occurrences of an Element in an Array

Count All Occurrences 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 write a program to count all occurrences of an element in an array.

Example (Count All Occurrences of an Element in an Array):

INPUT:
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
OUTPUT:
The number of occurrences of 12 are: 3

The steps required to count all occurrences of an element in an array 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 all occurrences of an element in an array is as follows:

/* Program to count all occurrences of an element in an array */  
#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    /* Scan size of array */  
    int n;  
    cout<<"Enter the size of an array: ";  
    cin>>n;  
      
    /* Scan Array Elements */  
    int arr[n];  
    cout<<"\nEnter array elements:\n";  
    for(int i = 0; i < n; i++)  
    {  
        cin>>arr[i];  
    }  
      
    /* Scan key element */  
    int key;  
    cout<<"\nEnter key element: ";  
    cin>>key;  
      
    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;  
}  

OUTPUT:
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 Add two Matrix.
  10. Program to Transpose a Matrix.
  11. Program to Multiply Two Matrix.
  12. Program to check Identity Matrix.
  13. Program to print Alternate Elements of an Array.
  14. Program to swap kth element from beginning to kth element from end in an Array.
  15. Program to print all possible subarrays of the given array.
  16. Program to print kth smallest and kth largest element of an Array.
  17. Program to find equilibrium index of an Array.
  18. Program to find majority element of an Array.
  19. Program to find mean of the Array.
  20. Program to sort an Array of 0s and 1s.

You may also like...

Leave a Reply

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