Optimizing counting number of occurrences of a given number in an array

It’s a good method if you know an upper bound on the range of values, for example if the values are of type Int8, or represent some limited physical or logical quantity.