There is a .txt file, there are many lines in it, == long, the lines can be repeated, but not in turn: not 1st and 2nd == but let's say the 14th and 49th line == , Question, how to find identical strings, but display these == strings once, I tried through Enumberble<strings> Repeat , but it only created copies of them ..
- if (! List.Contains (str)) {}, but if there are too many lines, with each new call the method will run slower. - QuaternioNoir
- Even as an option, read everything in the list, and then distinct get nonrecurring values. Also not the fastest way. - QuaternioNoir
- @QuaternioNoir will try ... - komra23
|
2 answers
For the case when you need to display on the screen, a slightly more efficient option without intermediate materialization:
IEnumerable<string> allLines = File.ReadLines(path); IEnumerable<string> distinctLines = allLines.Distinct(); foreach (var line in distinctLines) Console.WriteLine(line); or simply
foreach (var line in File.ReadLines(path).Distinct()) Console.WriteLine(line); In case you need to output to a file, you will need one materialization:
File.WriteAllLines( path, File.ReadLines(path).Distinct().ToList()); (otherwise the file will not be closed when you try to write to it).
|
I will write in steps to make it clearer:
// читаем все данные из файла string[] lines = File.ReadAllLines(путь); // преобразуем в список var list = new List<string>(lines); // получаем только уникальные элементы var uniqueStrings = list.Distinct(); // записываем их обратно File.WriteAllLines(путь, uniqueStrings); |