The first zeros the counts, the second then counts. If you have the same index multiple times (which is the point of counting when you want to scale by frequency), you cannot combine the two.
My guess for the first loop would be that it is cheaper to only initialize those indices that are actually used rather than all of them en bloc.
Best regards
Thomas