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 204427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:28:00+00:00 2026-05-11T17:28:00+00:00

I have two (very large) text files. What is the fastest way – in

  • 0

I have two (very large) text files. What is the fastest way – in terms of run time – to create a third file containing all lines of file1 that do not appear in file2?

So if file1 contains:

Sally  
Joe  
Tom  
Suzie

And file2 contains:

Sally  
Suzie  
Harry  
Tom

Then the output file should contain:

Joe
  • 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-11T17:28:00+00:00Added an answer on May 11, 2026 at 5:28 pm

    Create a hashmap containing each line from file 2. Then for each line in file 1, if it is not in the hashmap then output it. This will be O(N), which is the best efficiency class you can achieve given that you have to read the input.

    Perl implementation:

    #!/usr/bin/env perl
    use warnings;
    use strict;
    use Carp ();
    
    my $file1 = 'file1.txt';
    my $file2 = 'file2.txt';
    
    my %map;
    {
        open my $in, '<',$file2 or Carp::croak("Cant open $file2");
        while (<$in>) {
          $map{$_} = 1;
        }
        close($in) or Carp::carp("error closing $file2");
    }
    {
       open my $in,'<', $file1 or Carp::croak("Cant open $file1");
       while (<$in>) {
        if (!$map{$_}) {
          print $_;
        }
       }
       close $in or Carp::carp("error closing $file1");
    }
    

    If file 2 is so large that the hashmap doesn’t fit in memory, then we have a different problem at hand. A possible solution is then to use the above solution on chunks of file 2 (small enough to fit into memory), outputing the results to temporary files. Provided there are sufficient matches between file 1 and file 2, then total output should be of reasonable size. To calculate the final results, we perform an intersection of the lines in temporary files, i.e. for a line to be in the final results it must occur in every temporary file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On a Linux system, I have a very large text file and I need
I have two very large(30000+ documents) collections, one contains words extracted from a text
I am comparing substrings in two large text files. Very simple, tokenizing into two
I have a very large comma-seperated text-file. I need to display this in a
I have two arrays, one is very large (more than million entries) and other
I have two very similar programs each trying to run two threads OddThread and
I have a very simple table with two columns, but has 4.5M rows. CREATE
I have a series of large text files (up to 1 gig) that are
At the moment, I have two very large DataTemplate objects to display two sets
As part of my work, I am working with very large text files and,

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.