Using StreamReader to count duplicates?

2019-06-12 07:41发布

I'm using streamreader now to read a file of people names, it is a text file, of people first names, so there are obviously duplicates, and i want to be able to display how many people have the same now so for example:

josh
alex
josh
john
alex

I want it to say,

josh 2
alex 2
john 1

but I can't seem to find an easy way of doing this, what would be the easiest way about doing this,

7条回答
我命由我不由天
2楼-- · 2019-06-12 08:42

Using an HashMap is a solution to your problem. When you read a name, check if the key is already present, if so, update it (+1), if not add it to your Hash Map.

In the end, all you need to do is print the key-value pairs.

查看更多
登录 后发表回答