
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the frequency of a number in an array using C++.
Suppose we have an array. There are n different elements. We have to check the frequency of one element in the array. Suppose A = [5, 12, 26, 5, 3, 4, 15, 5, 8, 4], if we try to find the frequency of 5, it will be 3.
To solve this, we will scan the array from left, if the element is the same as the given number, increase the counter, otherwise go for the next element, until the array is exhausted.
Example
#include<iostream> using namespace std; int countElementInArr(int arr[], int n, int e) { int count = 0; for(int i = 0; i<n; i++){ if(arr[i] == e) count++; } return count; } int main () { int arr[] = {5, 12, 26, 5, 3, 4, 15, 5, 8, 4}; int n = sizeof(arr)/sizeof(arr[0]); int e = 5; cout << "Frequency of " << e << " in the array is: " << countElementInArr(arr, n, e); }
Output
Frequency of 5 in the array is: 3
- Related Articles
- Find the frequency of a digit in a number using C++.
- Write a Golang program to find the frequency of an element in an array
- Find the Number of Prime Pairs in an Array using C++
- Find the Number of Unique Pairs in an Array using C++
- Find frequency of smallest value in an array in C++
- Write a Golang program to find the frequency of each element in an array
- Find Duplicate Elements and its Frequency in an Array in Java
- Count number of occurrences (or frequency) in a sorted array in C++
- How to find the frequency of a particular word in a cell of an excel table using Python?
- Sorting array of Number by increasing frequency JavaScript
- Write a program to find the first non-repeating number in an integer array using Java?
- Find the only missing number in a sorted array using C++
- Building frequency map of all the elements in an array JavaScript
- Find the number of times a value of an object property occurs in an array with JavaScript?
- Find the number of elements greater than k in a sorted array using C++

Advertisements