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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:56:17+00:00 2026-06-01T15:56:17+00:00

I have a 2GB big text file, it has 5 columns delimited by tab.

  • 0

I have a 2GB big text file, it has 5 columns delimited by tab.
A row will be called duplicate only if 4 out of 5 columns matches.

Right now, I am doing dduping by first loading each coloumn in separate List
, then iterating through lists, deleting the duplicate rows as it encountered and aggregating.

The problem: it is taking more than 20 hours to process one file.
I have 25 such files to process.

Can anyone please share their experience, how they would go about doing such dduping?

This dduping will be a throw away code. So, I was looking for some quick/dirty solution, to get job done as soon as possible.

Here is my pseudo code (roughly)

Iterate over the rows
  i=current_row_no.    
    Iterate over the row no. i+1 to last_row
                    if(col1 matches  //find duplicate
                        && col2 matches
                        && col3 matches  
                        && col4 matches)
                        { 
                           col5List.set(i,get col5); //aggregate 
                        }

Duplicate example

A and B will be duplicate A=(1,1,1,1,1), B=(1,1,1,1,2), C=(2,1,1,1,1) and output would be A=(1,1,1,1,1+2) C=(2,1,1,1,1) [notice that B has been kicked out]

  • 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-06-01T15:56:18+00:00Added an answer on June 1, 2026 at 3:56 pm

    A HashMap will be your best bet. In a single, constant time operation, you can both check for duplication and fetch the appropriate aggregation structure (a Set in my code). This means that you can traverse the entire file in O(n). Here’s some example code:

    public void aggregate() throws Exception
      {
        BufferedReader bigFile = new BufferedReader(new FileReader("path/to/file.csv"));
    
        // Notice the paramter for initial capacity. Use something that is large enough to prevent rehashings.
        Map<String, HashSet<String>> map = new HashMap<String, HashSet<String>>(500000);
    
        while (bigFile.ready())
        {
          String line = bigFile.readLine();
          int lastTab = line.lastIndexOf('\t');
          String firstFourColumns = line.substring(0, lastTab);
    
          // See if the map already contains an entry for the first 4 columns
          HashSet<String> set = map.get(firstFourColumns);
    
          // If set is null, then the map hasn't seen these columns before
          if (set==null)
          {
            // Make a new Set (for aggregation), and add it to the map
            set = new HashSet<String>();
            map.put(firstFourColumns, set);
          }
    
          // At this point we either found set or created it ourselves
          String lastColumn = line.substring(lastTab+1);
          set.add(lastColumn);
        }
        bigFile.close();
    
        // A demo that shows how to iterate over the map and set structures
        for (Map.Entry<String, HashSet<String>> entry : map.entrySet())
        {
          String firstFourColumns = entry.getKey();
          System.out.print(firstFourColumns + "=");
    
          HashSet<String> aggregatedLastColumns = entry.getValue();
          for (String column : aggregatedLastColumns)
          {
            System.out.print(column + ",");
          }
          System.out.println("");
        }
      }
    

    A few points:

    • The initialCapaticy parameter for the HashMap is important. If the number of entries gets bigger than the capacity, then the structure is re-hashed, which is very slow. The default initial capacity is 16, which will cause many rehashes for you. Pick a value that you know is greater than the number of unique sets of the first four columns.
    • If ordered output in the aggregation is important, you can switch the HashSet for a TreeSet.
    • This implementation will use a lot of memory. If your text file is 2GB, then you’ll probably need a lot of RAM in the jvm. You can add the jvm arg -Xmx4096m to increase the maximum heap size to 4GB. If you don’t have at least 4GB this probably won’t work for you.
    • This is also a parallelizable problem, so if you’re desperate you could thread it. That would be a lot of effort for throw-away code, though. [Edit: This point is likely not true, as pointed out in the comments]
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 2GB text file on my linux box that I'm trying to
I have a very big .txt file of millions of strings and it has
I have a file of size 2GB which has student records in it. I
I have a 1.2GB file that contains a one line string. What I need
Duplicate data removal using Perl called within via a batch file within Windows A
I have a question. Anybody try to open big AVI files (>= 2GB) in
I have a very large data file (2GB-3GB). I need to parse some data
I have a huge XML file, about 2GB in size, containing Resumes. There are
Say I want to have a server that can accept 2GB file over network,
I work in Linux. I have 3 file (about 2Gb each containing human genome

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.