Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 303997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:14:23+00:00 2026-05-12T07:14:23+00:00

Given an input file of text lines, I want duplicate lines to be identified

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:14:23+00:00Added an answer on May 12, 2026 at 7:14 am

    This should do (and will copy with large files).

    Note that it only removes duplicate consecutive lines, i.e.

    a
    b
    b
    c
    b
    d
    

    will end up as

    a
    b
    c
    b
    d
    

    If you want no duplicates anywhere, you’ll need to keep a set of lines you’ve already seen.

    using System;
    using System.IO;
    
    class DeDuper
    {
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("Usage: DeDuper <input file> <output file>");
                return;
            }
            using (TextReader reader = File.OpenText(args[0]))
            using (TextWriter writer = File.CreateText(args[1]))
            {
                string currentLine;
                string lastLine = null;
    
                while ((currentLine = reader.ReadLine()) != null)
                {
                    if (currentLine != lastLine)
                    {
                        writer.WriteLine(currentLine);
                        lastLine = currentLine;
                    }
                }
            }
        }
    }
    

    Note that this assumes Encoding.UTF8, and that you want to use files. It’s easy to generalize as a method though:

    static void CopyLinesRemovingConsecutiveDupes
        (TextReader reader, TextWriter writer)
    {
        string currentLine;
        string lastLine = null;
    
        while ((currentLine = reader.ReadLine()) != null)
        {
            if (currentLine != lastLine)
            {
                writer.WriteLine(currentLine);
                lastLine = currentLine;
            }
        }
    }
    

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

    static void CopyLinesRemovingAllDupes(TextReader reader, TextWriter writer)
    {
        string currentLine;
        HashSet<string> previousLines = new HashSet<string>();
    
        while ((currentLine = reader.ReadLine()) != null)
        {
            // Add returns true if it was actually added,
            // false if it was already there
            if (previousLines.Add(currentLine))
            {
                writer.WriteLine(currentLine);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program produces the output I expect when given an input file of text
I've got a form uploading input lines of text to a .txt file and
I have a very large (~8 gb) text file that has very long lines.
Given the input XML file: <acctInfo> <wfInfo> <aaa>1</aaa> <bbb>1</bbb> <ccc>1</ccc> <ddd>1</ddd> <eee>1</eee> </wfInfo> <acctInfo>
Given some source file (or more generic - input stream), I need to find
Suppose that my Haskell function is given an input, which is supposed to be
I need to export some numeric values from a given ASCII text file and
I'm creating file(images) upload form. I want to add input elements by clicking on
I am making a simple chat bot in Python. It has a text file
Let's say I have an input text file of the following format: Section1 Heading

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.