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

  • SEARCH
  • Home
  • 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 6585981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:43:57+00:00 2026-05-25T16:43:57+00:00

I have two text files files (TXT) which contain over 2 million distinct file

  • 0

I have two text files files (TXT) which contain over 2 million distinct file names. I want to loop through all the names in the first file and find those that are also present in the second text file.

I have tried looping through the StreamReader but it takes a lot of time. I also tried the code below, but it still takes too much time.

StreamReader first = new StreamReader(path);
string strFirst = first.ReadToEnd();
string[] strarrFirst = strFirst.Split('\n');

 bool found = false;

StreamReader second = new StreamReader(path2);
string str = second.ReadToEnd();
string[] strarrSecond = str.Split('\n');

for (int j = 0; j < (strarrFirst.Length); j++)
{
          found = false;

    for (int i = 0; i < (strarrSecond .Length); i++)
    {
        if (strarrFirst[j] == strarrSecond[i])
        {
            found = true;
            break;
        }
    }

    if (!found)
    {
        Console.WriteLine(strarrFirst[j]);
    }
}

What is a good way to compare the files?

  • 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-25T16:43:58+00:00Added an answer on May 25, 2026 at 4:43 pm

    How about this:

    var commonNames = File.ReadLines(path).Intersect(File.ReadLines(path2));
    

    That’s O(N + M) instead of your current solution which tests every line in the first file with every line in the second file – O(N * M).

    That’s assuming you’re using .NET 4. Otherwise, you could use File.ReadAllLines, but that will read the whole file into memory. Or you could write the equivalent of File.ReadLines yourself – it’s not terribly hard.

    Ultimately you’re likely to be limited by file IO by the time you’ve got rid of the O(N * M) problem in your current code – there’s not much way to get round that.

    EDIT: For .NET 2, first let’s implement something like ReadLines:

    public static IEnumerable<string> ReadLines(string file)
    {
        using (TextReader reader = File.OpenText(file))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                yield return line;
            }
        }
    }
    

    Now we really want to use a HashSet<T>, but that wasn’t in .NET 2 – so let’s use Dictionary<TKey, TValue> instead:

    Dictionary<string, string> map = new Dictionary<string, string>();
    foreach (string line in ReadLines(path))
    {
        map[line] = line;
    }
    
    List<string> intersection = new List<string>();
    foreach (string line in ReadLines(path2))
    {
        if (map.ContainsKey(line))
        {
            intersection.Add(line);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two 3GB text files, each file has around 80 million lines. And
I have two text files. hash_only.txt and final_output.txt hash_only.txt looks like below. 193548 401125
I have two text file. file1.txt has: gedit google chrome git vim foo bar
I have two text files that contain columnar data of the variety position -
I have two text files, I want to place a text in the middle
Hi I have two text files that each contain different information about certain structures.The
i have two text files file 1 number,name,account id,vv,sfee,dac acc,TDID 7000,john,2,0,0,1,6 7001,elen,2,0,0,1,7 7002,sami,2,0,0,1,6 7003,mike,1,0,0,2,1
I have two text files. hash_only.txt and final_output.txt hash_only.txt looks like below. 193548 401125
I have several threads which need to write to two different text files. So
I have a text.txt file which contains following txt. Kontagent Announces Partnership with Global

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.