- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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)

#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)

# 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

- Related Articles
- Count occurrences of the average of array elements with a given number in C++
- Count of smaller or equal elements in the sorted array in C++
- Absolute distinct count in a sorted array in C++?
- How to count number of occurrences of repeated names in an array - JavaScript?
- Count smaller elements in sorted array in C++
- Count 1’s in a sorted binary array in C++
- Count Occurrences of Anagrams in C++
- Count elements less than or equal to a given value in a sorted rotated array in C++
- Find element in a sorted array whose frequency is greater than or equal to n/2 in C++.
- Absolute distinct count in a sorted array?
- Count of only repeated element in a sorted array of consecutive elements in C++
- Count subarrays with equal number of occurrences of two given elements in C++
- Find the Rotation Count in Rotated Sorted array in C++
- Count occurrences of a character in a repeated string in C++
- Count the number of occurrences of a string in a VARCHAR field in MySQL?