There is some magic in life. Found a way to count number of occurances of all elements in array (only if they are sorted). Basic idea was:

  • Read the values in the array
  • Sort the array
  • Enumerate the array, and ignore (but count) any elements matching the previous element. reset the counter when you discover a new element.

Let array[10] => array

std::sort(array, array+10)
for (const int *it = array; it != array+i;) {
    unsigned int count = 1;
    int value = *it;
    for (++it; (it != array+10) && *it == value; ++count, ++it);
//might wana print count if you want to;

Original link: