Program to count occurences of each elements in an array

I have to write a program to count the occurrence of each element in array??plz hlp me?
Well, what's your first attempt ? Any idea ? No one will do it for you.
You can use standard container std::map for this purpose.

For example (without testing)

1
2
3
4
5
6
7
8
9
10
11
12
const int N = 20;
int a[N];

srand( time( 0 ) );

std::generate( std::begin( a ), std::end( a ), []{ return ( rand() % ( N / 4 ) ); } );

std::map<int, unsigned int> m;

for ( auto x : a ) ++m[x];

for ( auto p : m ) std::cout << p.first << '\t' << p.second << std::endl;
Last edited on
Write down the process then code it, show us what you can do.

1 idea
First you need to know the size of the array
create a while loop inside a for loop
see if array[x] matches array [y] then subtract 1 from your total because array[0] should always match array[0].

Last edited on
Topic archived. No new replies allowed.