Count number of occurrences (or frequency) in a sorted array in C++


We are given a sorted array of integer type elements and the number let’s say, num and the task is to calculate the count of the number of times the given element num is appearing in an array.

Input − int arr[] = {1, 1, 1,2, 3, 4}, num = 1

Output − Count of number of occurrences (or frequency) in a sorted array are − 3

Input − int arr[] = {2, 3, 4, 5, 5, 6, -7}, num = 5

Output − Count of number of occurrences (or frequency) in a sorted array are − 2

Input − int arr[] = {-1, 0, 1, 2, 3}, num = 7

Output − Count of number of occurrences (or frequency) in a sorted array are − 0

Approach used in the below program is as follows

There can be multiple approaches that can be followed to solve the above problem.

Naive approach

  • Declare an array of integer elements containing both positive and negative numbers and an integer variable num of which we have to find the frequency in an array.

  • Calculate the size of an array and pass all the data to the function for further processing.

  • Declare a temporary variable count to store the count of number of times the variable num is appearing

  • Start loop FOR from i to 0 till the size of an array

  • Inside the loop, check IF num = arr[i] then increment the value of count by 1

  • Return the count

  • Print the result.

Efficient approach

  • Declare an array of integer elements containing both positive and negative numbers and an integer variable num of which we have to find the frequency in an array.

  • Calculate the size of an array and pass all the data to the function for further processing.

  • Declare a temporary variable count to store the count of number of times the variable num is appearing

  • Set a pointer first as lower_bound(arr, arr+size, num)

  • Check IF first = (arr + size) || (*first != num) then return 0

  • Set end pointer as upper_bound(first, arr+size, num)

  • Set count as last - first

  • Return count

  • Print result

Example (naive approach)

 Live Demo

#include <iostream>
using namespace std;
int frequency_count(int arr[], int num, int size){
   int count = 0;
   for(int i=0; i<size; i++){
      if(num==arr[i]){
         count++;
      }
   }
   return count;
}
int main(){
   int arr[] = {1, 1, 1,2, 3, 4};
   int num = 1;
   int size = sizeof(arr)/sizeof(arr[0]);
   cout<<"Count of number of occurrences (or frequency) in a sorted array are: "<<frequency_count(arr, num, size);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of number of occurrences (or frequency) in a sorted array are: 3

Example (Efficient Approach)

 Live Demo

# include <bits/stdc++.h>
using namespace std;
int frequency_count(int arr[], int num, int size){
   int *first = lower_bound(arr, arr+size, num);
   if (first == (arr + size) || *first != num){
      cout<<"The Element is not present in an array ";
      return 0;
   }
   int count = 0;
   int *last = upper_bound(first, arr+size, num);
   count = last - first;
   return count;
}
int main(){
   int arr[] = {1, 1, 1, 2, 3, 4};
   int num = 1;
   int size = sizeof(arr)/sizeof(arr[0]);
   cout<<"Count of number of occurrences (or frequency) in a sorted array are: "<<frequency_count(arr, num, size);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of number of occurrences (or frequency) in a sorted array are: 3

Updated on: 31-Aug-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements