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(); foreach (var v in result) { File.AppendAllText("finalResult.txt", v + "\n"); } } 

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). How can I implement this removal?

 class PartialComparer : IEqualityComparer<string> { public string GetComparablePart(string s) { return s.Split('@')[1]; } public bool Equals(string x, string y) { return GetComparablePart(x).Equals(GetComparablePart(y)); } public int GetHashCode(string obj) { return GetComparablePart(obj).GetHashCode(); } } 
  • Overwrite the file with the contents where only the necessary (remaining) values ​​would be. Use File.WriteAllLines() instead of File.AppendAllText() . - BlackWitcher
  • @BlackWitcher I meant that I need finalResult "now", and after some time from inputData I will again take unique values ​​and write to finalResult. - cruim
  • And what's stopping to do that now? Take inputData.txt , read the List , add the contents of finalResult.txt , use Distinct , save overwriting finalResult.txt . Repeat if necessary. Salt and sugar to taste :-) - BlackWitcher
  • @BlackWitcher is initially in the inputData.txt there are duplicate lines, I need it to remove only 1 version of such a line. those. after the first iteration of the list (1,2,3,4,1,2) should remain (1,2) in inputData.txt - cruim
  • one
    @BlackWitcher added implementation code to question - cruim

2 answers 2

Well, I can offer a bike

  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().ToList(); if (list == null && list.Count==0) return; List<int> indexes = new List<int>(); for (int i = 0; i < list.Count - 1; i++ ) { if(i<result.Count && list.Contains(result[i])) { indexes.Add(list.IndexOf(result[i])); } } int delIndex = 0; foreach (int i in indexes) { list.RemoveAt(i + delIndex); delIndex--; } //list - с удаленными значениями foreach (var v in result) { File.AppendAllText("finalResult.txt", v + "\n"); } } 
  • @cruim I run this code with a list (1.2.3.4.1.2.2) in my list I have 1.2.2 as it should be by condition! - Yury Bakharev
  • That's right. Added only File.Delete ("inputData.txt"); foreach (var v in list) {File.AppendAllText ("inputData.txt", v + "\ n"); } - cruim

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 :)

  • Not quite correctly. with input data (1,2,3,4,1,2,2), the output will be (1,2), but not (1,2,2) - cruim
  • @ cruim bike rides on this sample! - Yury Bakharev
  • @YuryBakharev specifically checked again, if the source file has more than two identical lines, then it will still overwrite them in a single copy. - cruim
  • Updated the answer, adding it. - BlackWitcher