Given an input file of text lines, I want duplicate lines to be identified and removed. Please show a simple snippet of C# that accomplishes this.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This should do (and will copy with large files).
Note that it only removes duplicate consecutive lines, i.e.
will end up as
If you want no duplicates anywhere, you’ll need to keep a set of lines you’ve already seen.
Note that this assumes
Encoding.UTF8, and that you want to use files. It’s easy to generalize as a method though:(Note that that doesn’t close anything – the caller should do that.)
Here’s a version that will remove all duplicates, rather than just consecutive ones: