使用 StreamReader 计算重复项? [英] Using StreamReader to count duplicates?
本文介绍了使用 StreamReader 计算重复项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在正在使用 streamreader 读取人名文件,它是一个文本文件,人名,所以显然有重复,我希望能够显示有多少人现在有相同的名字例如:
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
我想说,
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,
推荐答案
我想说使用 Dictionary
.
Dictionary<string, int> firstNames = new Dictionary<string, int>();
foreach (string name in YourListWithNames)
{
if (!firstNames.ContainsKey(name))
firstNames.Add(name, 1);
else
firstNames[name] += 1;
}
当然有很多不同的解决方法,但这就是我要解决的方法.我还没有运行这段代码,但我认为这会对你有所帮助.
Of course there are many different paths to a solution, but this is how I would tackle it. I haven't run this code yet, but this will help you I think.
这篇关于使用 StreamReader 计算重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文