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 SinghRank #66 · 26.2K reputationPosted Jan 3, 2023, 3:39 AM EST
string str = File.ReadAllText(@"yourpath\textFilename.txt"); string[] words = str.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); Dictionary repts= words .GroupBy(word => word) .ToDictionary( kvp => kvp.Key, kvp => kvp.Count());Rajesh GamiRank #30 · 41.5K reputationPosted Jan 3, 2023, 4:43 AM EST
Hello Anand
Easiest way to use LINQ
string[] lines = File.ReadAllLines(path); lines = lines.GroupBy(x => x).Where(g => g.Count() > 1).Select(g => g.Key).ToArray(); File.WriteAllLines(newPath, lines):Aravind GovindarajRank #430 · 5.8K reputationPosted Jan 3, 2023, 4:29 AM EST
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
Sandhiya PriyaRank #26 · 43.2K reputationPosted Jan 6, 2026, 11:18 PM EST
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
using System; using System.Collections.Generic; using System.IO; class Program { static void Main() { string filePath = @"\\sharedpath\yourfile.txt"; // shared path var counts = new Dictionary(StringComparer.OrdinalIgnoreCase); // Read line by line (streaming) using (var reader = new StreamReader(filePath)) { string line; while ((line = reader.ReadLine()) != null) { if (counts.ContainsKey(line)) counts[line]++; else counts[line] = 1; } } // Find duplicates foreach (var kvp in counts) { if (kvp.Value > 1) { Console.WriteLine($"Text: {kvp.Key}, Count: {kvp.Value}"); } } } }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):
var duplicates = File.ReadLines(filePath) .GroupBy(line => line) .Where(g => g.Count() > 1) .Select(g => new { Text = g.Key, Count = g.Count() });Loads more into memory, not ideal for 5.4M records.
Database approach: Bulk insert into a table, then run:
SELECT Text, COUNT(*) FROM Records GROUP BY Text HAVING COUNT(*) > 1;Handles huge files efficiently.