How to Count the Number of Occurrences of a Value in an Array in C++? (original) (raw)
Last Updated : 18 Mar, 2024
In C++, an array is a data structure that stores the collection of the same type of data in a contiguous memory location. In this article, we will learn how to count the number of occurrences of a value in an array in C++.
**Example:
**Input:
arr= {2, 4, 5 ,2 ,4 , 5, 2 , 3 ,8}
Target = 2
**Output:
Number of occurences of 2 are : 3
Count the Number of Occurrences in an Array in C++
To count the number of occurrences of a specific value in an array, we can use a simple for loop while looking for our target value. If the target value is found, we increment the counter variable. We do this till the whole array is scanned.
**Approach
- Initialize a counter variable set to zero.
- Iterate through the array and check if the current element matches the target value.
- If it matches, increment the counter by one.
- Else continue to the next iteration.
- Repeat the process until all elements in the array have been checked.
- Finally, return the counter.
**C++ Program to Count the Number of Occurrences of a Value in an Array
The below example demonstrates how we can count the total number of occurrences of a value in an array.
C++ `
// C++ Program to illustrate how to count the number of // occurrences of a value in an array #include using namespace std;
int main() {
// initializating an Array
int arr[] = { 2, 4, 5, 2, 4, 5, 2, 3, 8 };
// Calculating the size of the array
int n = sizeof(arr) / sizeof(arr[0]);
// Defining the target number to search for
int target = 2;
// Initialize a counter
int counter = 0;
// Loop through the array elements
for (int i = 0; i < n; i++) {
// Check if the current element equals the target
// number
if (arr[i] == target) {
counter++;
}
}
// Output the result
cout << "Number " << target << " occurs " << counter
<< " times in the array.";
return 0;
}
`
Output
Number 2 occurs 3 times in the array.
**Time Complexity: O(n), here n is the number of elements in the array.
**Auxilliary Space: O(1)
Similar Reads
- C++ Program to Find Index of First Occurrence of a Value in Array In C++, an array is a data structure that stores elements of the same type in contiguous memory locations. In this article, we will learn how to find the index of the first occurrence of a specific value in an array in C++. Example: Input: int arr[] = {5, 7, 1, 2, 3, 7, 1} Target = 1Output: The firs 2 min read
- How to Find the Mode of Numbers in an Array in C++? Mode of any dataset is the item that occurs most frequently in it. In this article, we will find the mode of numbers in an unsorted array in C++. For Example,Input: myArray = { 1, 2, 3, 4, 5, 2, 3, 2, 2, 4, 2 } Output: Mode : 2Finding Mode of Array Elements in C++To find the mode of the array elemen 2 min read
- How to Find the Second Occurrence of an Element in Vector in C++? In C++, vector containers store the data in a contiguous memory location like arrays and also can resize themselves to store more elements. In this article, we will learn how to find the second occurrence of a specific element in a vector in C++. Example Input: myVector = {20, 30, 10, 50, 10, 80, 10 2 min read
- How to Find the Last Occurrence of an Element in a Set in C++? In C++, a set is a container that stores unique elements in a sorted order and elements are accessed and traversed using iterators. In this article, we will learn how to find the last occurrence of a specific element in a set in C++. Example Input:set s = {1, 2, 3, 4, 5, 6, 7, 8, 9}; Key 2 min read
- How to Find All Occurrences of an Element in a Set in C++? Finding the all occurrences of a specific element in a set using the C++ STL is a very efficient process that is done with the help of std:📐:distance() member function. In this article, we'll explore how to find the first element in a set using the C++ STL. For Example,Input:mySet = {1, 2, 4, 3, 2 min read
- How To Find All Occurrences of a Key in a Multimap in C++? In C++, multimaps are associative containers similar to maps, but unlike maps, they can store multiple values mapped to the same key. In this article, we will learn how to find all the occurrences of a specific key in a multimap in C++. Example: Input:myMutimap = {{ "id", "111" }, { "id", "112" }, { 2 min read
- Count the number of 1's and 0's in a binary array using STL in C++ ? Given a binary array, the task is to count the number of 1's and 0's in this array using STL in C++. Examples: Input: arr[] = {1, 0, 0, 1, 0, 0, 1} Output: 1's = 3, 0's = 4 Input: arr[] = {1, 1, 1, 1, 0, 0, 1} Output: 1's = 5, 0's = 2 Approach: We can count the same using count_if() function present 1 min read
- How to Find All Occurrences of an Element in a Multiset in C++? In C++, a multiset is a container similar to a set but it allows multiple occurrences of its elements i.e. duplicate values. In this article, we will learn how to find all occurrences of a specific element in a multiset in C++. Example: Input: myMultiset = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};target = 3Ou 2 min read
- Count the number of clumps in the given Array Given an array arr[] of N integers, the task is to count the number of clumps in the given array. Clump is defined as a series of 2 or more adjacent elements of the same value. Examples: Input: arr[] = { 13, 15, 66, 66, 37, 8, 8, 11, 52 }; Output: 2 Explanation: There are two clumps in the given arr 5 min read
- How to Find the Mode in a Sorted Array in C++? The mode of the given numbers can be defined as the value that occurs the most in the given dataset or the value with the highest frequency. In this article, we will discuss how to calculate the mode of the numbers in a sorted array in C++. Example: Input: myVector = {1, 2, 2, 3, 3, 3, 4, 4, 5} Outp 3 min read