Hello All,
Need your help on , how to find duplicate text in .txt file placed in shared path.
Example let if text last 4000 record text as "anand" and 40001 record also contains "anand" I need total count of duplicate records count exist in this .txt file using C#.net .
total records 54 lakhs records exist in .txt file but for each record any performance issue comes please suggest any other example.
Sachin SinghPosted Jan 3, 2023, 8:39 AM
Sandhiya PriyaPosted Jan 7, 2026, 4:18 AM
you have a large text file (~5.4 million lines) in a shared path, and you want to find duplicate records (lines) and their counts using C#.NET, while keeping performance in mind.
Key Considerations
File size: 5.4 million lines can be hundreds of MBs ? must avoid loading entire file into memory at once.
Performance: Use streaming (line by line) instead of
File.ReadAllLines().Data structure: Use a Dictionary or ConcurrentDictionary to count occurrences.
Duplicates: After processing, filter entries where count > 1.
Efficient C# Example
Performance Tips
Streaming:
StreamReader.ReadLine()avoids loading the whole file into memory.Dictionary: O(1) average lookup ? efficient for millions of records.
Case sensitivity: Use
StringComparer.OrdinalIgnoreCaseif you want"Anand"and"anand"treated as duplicates.Memory: Each unique line is stored once. If lines are very long or unique, memory usage can grow.
If memory is a concern, consider writing counts to a temporary database (SQL Server, SQLite) instead of keeping everything in memory.
Example Output
If file contains:
Output:
Alternative Approaches
LINQ (simpler, less memory efficient):
Loads more into memory, not ideal for 5.4M records.
Database approach: Bulk insert into a table, then run:
Handles huge files efficiently.
Rajesh GamiPosted Jan 3, 2023, 9:43 AM
Hello Anand
Easiest way to use LINQ
Aravind GovindarajPosted Jan 3, 2023, 9:29 AM
try to use the HashSet approach which overcomes the performance issue. Please find the reference link
https://stackoverflow.com/questions/43016773/how-to-remove-duplicate-lines-from-a-large-text-file-efficiently