Skip to main content

Find duplicates in large file - java



I have really large file with approximately 15 million entries. Each line in the file contains a single string (call it key).





I need to find the duplicate entries in the file using java. I tried to use a hashmap and detect duplicate entries. Apparently that approach is throwing me a "java.lang.OutOfMemoryError: Java heap space" error.





How can I solve this problem?





I think I could increase the heap space and try it, but I wanted to know if there are better efficient solutions without having to tweak the heap space.


Comments

  1. The key is that your data will not fit into memory. You can use external merge sort for this:

    Partition your file into multiple smaller chunks that fit into memory. Sort each chunk, eliminate the duplicates (now neighboring elements).

    Merge the chunks and again eliminate the duplicates when merging. Since you will have an n-nway merge here you can keep the next k elements from each chunk in memory, once the items for a chunk are depleted (they have been merged already) grab more from disk.

    ReplyDelete
  2. I'm not sure if you'd consider doing this outside of java, but if so, this is very simple in a shell:

    cat file | sort | uniq

    ReplyDelete
  3. You probably can't load the entire file at one time but you can store the hash and line-number in a HashSet no problem.

    Pseudo code...

    for line in file
    entries.put(line.hashCode, line-number)
    for entry in entries
    if entry.lineNumbers > 1
    fetch each line by line number and compare

    ReplyDelete
  4. One way I can imagine solving this is to first use an external sorting algorithm to sort the file (searching for external sort java yields lots of results with code). Then you can iterate the file line by line, duplicates will now obviously be directly following each other so you only need to remember the previous line while iterating.

    ReplyDelete
  5. If you cannot build up a complete list since you don't have enough memory, you might try do it in loops. I.e. create a hashmap but only store a small portion of the items (for example, those starting with A). Then you gather the duplicates, then continue with 'B' etc.

    Of course you can select any kind of 'grouping' (i.e. first 3 characters, first 6 etc).

    It only will take (many) more iterations.

    ReplyDelete
  6. I don't think you need to sort the data to eliminate duplicates. Just use quicksort inspired approach.


    Pick k pivots from the data (unless your data is really wacky this should be pretty straightforward )
    Using these k pivots divide the data into k+1 small files
    If any of these chunks are too large to fit in memory repeat the process just for that chunk
    Once you have manageable sized chunks just apply your favorite method (hashing?) to find duplicates


    Note that k can be equal to 1.

    ReplyDelete
  7. You might try a Bloom filter, if you're willing to accept a certain amount of statistical error. Guava provides one, but there's a pretty major bug in it right now that should be fixed probably next week with release 11.0.2.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex