1.
If you know the range of values in the integer matrix 'm', say the values range form 10 to 1009
then you could create an array to hold the counts for each value
// initial all values to 0
int freq[1000] = {0};
//then you could run though the matrix
//and ...
++ freq[ m[i][j]-10 ];
/*
for each i, j in the nested inner loop
then skipping on 0 values in the freq array
just report the freq each value (index goes from 0, 999 but values are (index +10) )
*/
2.
Another way would be to use a struct that holds the value and the freq of that value
and a vector to hold the structs
the first time a values occurs (that is, that value is not already in the vector of struct)
push_back a struct with that value and the count = 1
every time an element in the matrix is aleady in the vector of struct, just ++ the count