posted 15 years ago
'good' depends on many things... how big will the real array be? what is the range of the expected values?
one way might be a map that uses the value as the key. each time you read a value, like '3', you check to see if that key/value is defined. if so, you increment the value. If not, you create it with a value of 1.
when you're done, you get a list of all the keys, and use that to get the counts.
if your range is sufficiently small, you could just define an int array of say (from your example) 15. Then, as you read each value, increment the int at that index by 1.
'good' is a balance between speed, readability, maintainability, and ease of programming.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors