views:

229

answers:

3

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
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
+1  A: 

Your code would look something like this:

  1. For each line in the input file.
  2. If the current line is not the same as the previous line, write it to the output file.
  3. 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
The OP clarified in a comment that they're not adjacent.
Matthew Flaschen