Added countBy function (similar to groupBy, but keeping a count of the occurrence of each key).