Count frequencies of one array; walk the other and consume counts.
Input
4 1 2 2 1 2 2 2
Output
2 2
3 4 9 5 5 9 4 9 8 4
4 9