What algorithm can you use to find duplicate phrases in a string?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Finding duplicate phrases in text can mean several different problems, and the algorithm depends on what you call a phrase. You might want repeated fixed-length token windows, repeated variable-length substrings, or near-duplicate expressions after normalization. Most production systems start with exact fixed-length duplicates using hash maps, then add more advanced methods only when requirements demand it. For production systems, phrase-definition clarity matters as much as algorithmic complexity because output semantics drive downstream decisions.
Define Phrase Rules Before Choosing an Algorithm
Before coding, lock down these rules:
- phrase length fixed or variable
- token-based or character-based matching
- case-sensitive or case-insensitive
- punctuation and whitespace normalization
If these rules are vague, two implementations can both look correct and still produce different results.
Algorithm 1: Hash Map on Token n-grams
For fixed phrase length such as 2-word or 3-word duplicates, token n-gram counting is simple and fast.
Complexity is linear in token count for fixed n, which scales well for large corpora.
Algorithm 2: Rolling Hash for Fixed-Length Windows
Rolling hash is useful for high-throughput character-window duplicate detection.
Hash collisions are possible, so direct substring verification is required for exact correctness.
Algorithm 3: Suffix Structures for Variable-Length Repeats
If phrase length is unknown and you need longest repeated substrings, suffix arrays or suffix automata are strong options. They are more complex but powerful for deep text analysis.
High-level suffix-array workflow:
- normalize text
- build suffix array
- compute longest-common-prefix values between adjacent suffixes
- extract repeated substrings above minimum length
This is often the right choice for advanced plagiarism checks or long-document mining.
Normalization Pipeline Has Huge Impact
Algorithm quality depends heavily on input normalization. Inconsistent normalization often creates false negatives.
Typical steps:
- lowercase conversion
- punctuation cleanup
- unicode normalization
- whitespace compression
Keep normalization rules versioned so downstream reports stay reproducible.
Include Positions, Not Just Counts
Duplicate detection output is far more useful when it includes locations.
Position data helps UI highlighting, reviewer workflows, and debugging.
Choosing the Right Method in Practice
Use this rule of thumb:
- exact fixed token length duplicates: n-gram hash map
- exact fixed character windows at high volume: rolling hash plus verification
- variable-length repeated substrings: suffix array or suffix automaton
Most business products can ship successfully with n-gram counting first, then upgrade if needed.
Common Pitfalls
A common mistake is skipping requirement definition and arguing about algorithm choice too early.
Another mistake is treating normalization as optional. Without it, duplicate quality is unstable across input sources.
A third mistake is using rolling hash results directly without collision checks.
Teams also return only aggregate counts and forget phrase positions, which limits usefulness for review tools.
Summary
- Duplicate phrase detection starts with clear phrase and normalization rules.
- Hash-map n-gram counting is usually the best first algorithm for fixed-length phrases.
- Rolling hash helps high-throughput window scanning when collision verification is added.
- Suffix-based structures are better for variable-length repeated substring analysis.
- Position-aware output makes duplicate findings more actionable.

