code for finding number of accurrences in arrays
what should be the code for finding number of occurrences in arrays?
You can simply iterate through an array and have a counter. Add 1 every time you find a value that you're looking for.
Here's how I would do it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
#include <array>
int main(){
std::array<T,9> array = {2,2,2,4,2,2,2,4,2};
int noOccurences = 0;
for(auto i:array){
if(i == 4)
noOccurences++;
}
std::cout << noOccurences;
return 0;
}
|
thank you
Topic archived. No new replies allowed.