Count number of individual lines from a txt file c#

1.3k Views Asked by At

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?

3

There are 3 best solutions below

0
On BEST ANSWER

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.

0
On

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.

0
On

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

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