The reason it is done this way, and the reason why the map is mutated now rather than something simpler like a length check, is so that the failure message is as good as possible. If the count is wrong in the loop, and the comparison is done this way, the failure message will include all of the pairs in the map that shouldn't have been there.