Evildommer5
Evildommer5

Reputation: 139

Count number of individual lines from a txt file c#

I have a txt document which has over 14000 different lines many of these are duplicates, is it possible to count the number of unique entries?

Upvotes: 4

Views: 1374

Answers (3)

Fischermaen
Fischermaen

Reputation: 12458

It's a simply "One-Liner" like that:

var lines = File.ReadAllLines("FileToRead.txt").Distinct().Count();

Edit: But take care with those kind of solutions. With files larger than 600 MB you might get problems.

Upvotes: 3

seth
seth

Reputation: 1389

Iterate through the file, save what you find in a collection, ignore already analyzed entries and in the end, just check the size of the collection.

Upvotes: -1

dtb
dtb

Reputation: 217263

You can use the File.ReadLines Method and LINQ's Distinct and Count Extension Methods:

var result = File.ReadLines("input.txt").Distinct().Count();

Upvotes: 11

Related Questions