3 ms·
One of the advantage of the array[item]++ / for (item in array) construct is that it avoids possibly expensive sorts (at the cost of generating potentially larg
by dredmorbius 3y ago
One of the advantage of the array[item]++ / for (item in array) construct is that it avoids possibly expensive sorts (at the cost of generating potentially large arrays), and tends to be quite fast.
That's even if you end up sorting the output by frequency, as the summary listing is typically shorter than the overall input data:
{a[$1]++}
END {for (i in array) printf("%6i %s\n", a[i], i) | "sort -k1nr | cat -n";}
The accumulator/loop idiom is ... fairly readily recognisable to someone familiar with awk.