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条回答
Animai°情兽
2楼-- · 2019-06-12 08:20

You could of course also do something like this (error checking omitted), using Linq:

var names = new List<string>(
    File.ReadAllText(pathToFile).Split(
    Environment.NewLine.ToCharArray(),
    StringSplitOptions.RemoveEmptyEntries
));
var namesAndOccurrences =
    from name in names.Distinct()
    select name + " " + names.Count(n => n == name);

foreach (var name in namesAndOccurrences)
    Console.WriteLine(name);

Depending on the size of the file, this might be desirable to get rid of the stream; however, this isn't to say that if the file was considerably large for memory that you should use ReadLine.

查看更多
对你真心纯属浪费
3楼-- · 2019-06-12 08:21
foreach (var keyvalue in File.ReadAllLines(@"C:\....").GroupBy(x => x).Select(x => new { name = x.Key, count = x.Count() }))
{
        Console.WriteLine(keyvalue.name + ": " + keyvalue.count);
}
查看更多
We Are One
4楼-- · 2019-06-12 08:29

Try this with LINQ.

First read your text file to a List<string> using this code:

const string f = "TextFile1.txt";

// 1
// Declare new List.
List<string> lines = new List<string>();

// 2
// Use using StreamReader for disposing.
using (StreamReader r = new StreamReader(f))
{
    // 3
    // Use while != null pattern for loop
    string line;
    while ((line = r.ReadLine()) != null)
    {
    // 4
    // Insert logic here.
    // ...
    // "line" is a line in the file. Add it to our List.
    lines.Add(line);
    }
}

You need to define a class where you will have name and accordingly the count:

class PersonCount
{
    public string Name { get; set; }
    public int Count { get; set; }
}

And finally use this Lambda expression to get desired List<string>

List<PersonCount> personCounts = lines.GroupBy(p => p).Select(g => new PersonCount() {Name = g.Key, Count = g.Count()}).ToList();

Now iterate through the list to get the names and the count of duplicates.

查看更多
forever°为你锁心
5楼-- · 2019-06-12 08:32

try this offline solution

StreamReader dr = new StreamReader(@"C:\txt.txt");
string str = dr.ReadToEnd();
string[] p = str.Split(new string[] { Environment.NewLine, " " }, StringSplitOptions.RemoveEmptyEntries);
Dictionary<string, int> count = new Dictionary<string, int>();
for (int i = 0; i < p.Length; i++)
{
    try
    {
        count[p[i].Trim()] = count[p[i]] + 1;
    }
    catch
    {
        count.Add(p[i], 1);
    }
}
查看更多
劫难
6楼-- · 2019-06-12 08:35

Store all names in a Dictionary<string, int> names.

Use something like this for each row:

var theName = reader.ReadLine();
names[theName] += 1;

(it should set the count to one if the item do not exist)

查看更多
放荡不羁爱自由
7楼-- · 2019-06-12 08:36

I'd say use a Dictionary<string, int>.

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.

查看更多
登录 后发表回答