How to find common strings among two very large files?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Finding common strings in two very large files is mostly a scaling problem, not a string-comparison problem. The right solution depends on whether one file fits in memory, whether the files are already sorted, and whether exact results matter more than speed or disk usage.
Start with the Simplest Useful Case
If one file is small enough to fit comfortably into memory, load it into a hash set and stream the other file line by line.
This runs in roughly linear time and gives fast membership tests, but memory usage is proportional to the number of unique strings in the in-memory file.
Use Sorting and Merge for Truly Large Inputs
If neither file fits in memory, sorting plus a merge pass is often the best exact strategy. The idea is the same as the merge step in merge sort.
- Sort both files externally if needed.
- Open both sorted files.
- Compare the current string from each file.
- Advance the pointer for the smaller string.
- Emit the string when both lines match.
Here is a streaming merge for already sorted files:
The merge step uses little memory because it keeps only the current lines in memory.
How to Sort Large Files
When files are bigger than RAM, external sorting is the standard answer. On Unix-like systems, the sort command already implements an external sort strategy efficiently:
comm -12 prints lines present in both sorted files. This is often the easiest production-grade answer if shell tools are allowed.
If you need the logic inside an application, chunked sorting plus temporary files can reproduce the same pattern, but it is much more code.
Think About Duplicates
Decide what “common strings” means for your use case.
- Unique intersection: each shared string appears once.
- Multiset intersection: shared strings appear as many times as they overlap.
The hash-set approach naturally leans toward unique intersection unless you track counts. The sorted merge approach can support either interpretation, depending on how you advance pointers and emit results.
Approximate Approaches Exist, But Use Them Intentionally
For extremely large data where exact intersection is too expensive, probabilistic structures such as Bloom filters can help screen likely matches. That can reduce I/O or memory use, but it introduces false positives unless followed by exact verification.
For most engineering tasks, exact set-based or sort-based methods are easier to explain and safer to trust.
Common Pitfalls
The most common mistake is reading both huge files fully into memory without checking actual size first. That can turn a simple job into a process that swaps or crashes.
Another issue is ignoring whether the files are already sorted. If they are sorted, you should exploit that and use a merge-based scan instead of building a large hash table.
People also forget about line normalization. Trailing spaces, newline differences, or case sensitivity can make logically identical strings appear different.
Finally, define duplicate behavior up front. “Common strings” sounds simple until someone asks whether repeated values should appear once or many times.
Summary
- Use a hash set when one file fits in memory and you need a simple exact solution.
- Use external sort plus a merge pass when both files are too large for memory.
- '
sortandcommare often the most practical exact tools on Unix-like systems.' - Normalize lines consistently before comparing them.
- Decide early whether you want unique intersection or duplicate-sensitive overlap.
Related reading
- How to find duplicates in 2 columns not 1
- How to find Local maxima in Kernel Density Estimation?
- How to find patterns lines, circles,... from a list of points?
- How to find probability distribution and parameters for real data?
- How to find convex hull in a 3 dimensional space
- How to find cycles of a given length in a directed graph? Using networkx
- How to find the installed pandas version
- How to find which columns contain any NaN value in Pandas dataframe

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.