how to delete same lines in file .txt but keep one of them using c# ?
+10
A:
Read line by line. Add the line to a HashSet, and Add returns true, print the line to the output.
Matthew Flaschen
2010-05-24 00:07:55
A:
There are many ways. One could be to use some version of a Set. Look at http://stackoverflow.com/questions/183685/c-set-collection for this.
If all the same lines are after one another, you could just iterate through and see if the line matches the last line. If it does, discard it.
Christian Jonassen
2010-05-24 00:09:02
+1
A:
Your code would look something like this:
- For each line in the input file.
- If the current line is not the same as the previous line, write it to the output file.
- Set the current line equal to the previous line.
This assumes that duplicate lines are adjacent. If they are not adjacent, you will have to sort the file first.
Turtle
2010-05-24 00:10:36
The OP clarified in a comment that they're not adjacent.
Matthew Flaschen
2010-05-24 00:39:08