I'll try without bikes, but with LINQ:
static void Main(string[] args) { List<string> list = new List<string>(); var files = "inputData.txt"; list = new List<string>(File.ReadAllLines(files)); //Заменим вашу строку: //var result = list.Distinct(new PartialComparer()).ToList(); //на var result = list.GroupBy(f => f).Where(gr => gr.Count() > 1).Select(g => g.Key); foreach (var v in result) { File.AppendAllText("finalResult.txt", v + "\n"); } }
It just turns out that from a list like this (1,2,3,4,1,2) in inputdata.txt will remain (1,2).
UPDATE:
This is how it works on any sequence of elements, deleting duplicate elements 1 time and ignoring non-repeating elements:
string s = "1,2,3,4,1,5,6,7,8,2,5,2,2,1"; //Вывод: 1,1,2,2,2,5 List<string> FileList = new List<string>(); FileList = s.Split(',').ToList(); FileList.Sort(); List<string> Collected = new List<string>(); for (int i = 0; i < FileList.Count - 1; i++) { if (FileList[i] == FileList[i + 1]) { Collected.Add(FileList[i]); } } //Вывод простенько, без затей: MessageBox.Show(string.Join("\n", Collected));
The only thing is that due to the sorting, the order of the elements is disturbed (they are sorted in the final list).
I write down unique values. At the same time I want the lines that I took to be deleted from the source file. If the source file is: 1,2,3,4,1,2,3. Then after deletion, it should remain: 1,2,3 (i.e., delete only one instance of the string).
This is exactly what the code does after the update - one copy of the line is taken from the source list, provided that the line appears in the list more than once.
If this also does not work as you would like, then a big request in the question is somehow more detailed (so that we would understand), to explain what you want to get at the output :)
File.WriteAllLines()instead ofFile.AppendAllText(). - BlackWitcherinputData.txt, read theList, add the contents offinalResult.txt, useDistinct, save overwritingfinalResult.txt. Repeat if necessary. Salt and sugar to taste :-) - BlackWitcher