How to Count the Number of Occurrences of a Value in an Array in C++? Last Updated : 18 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 = 2Output: Number of occurences of 2 are : 3Count 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. ApproachInitialize 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 ArrayThe 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 <iostream> 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; } OutputNumber 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) Comment More infoAdvertise with us Next Article How to Find All Occurrences of an Element in a Set in C++? B bhushanc2003 Follow Improve Article Tags : C++ Programs C++ cpp-array CPP Examples Practice Tags : CPP 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 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::set::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 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 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 How to Find the Mode in a 2D Array in C++? A mode is a number that occurs most frequently in comparison to other numbers in a given dataset. In this article, we will find the mode in a 2D array of integers in C++. Input:myArray = { {1, 2, 2, 3}, {3, 4, 5, 5}, {5, 6, 7, 8} }Output: 5Mode in 2D ArrayTo find a mode of numbers in a 2D array, we 2 min read Like