What Is Text Deduplication
Text deduplication removes duplicate lines, paragraphs, or content from text. It helps clean data, reduce redundancy, and improve text quality.
Deduplication Types
| Type | Description | Example |
|------|-------------|---------|
| Exact | Identical lines | abc + abc → abc |
| Case-insensitive | Ignore case differences | ABC + abc → abc |
| Trim whitespace | Ignore leading/trailing spaces | abc + abc → abc |
| Fuzzy | Similarity above threshold | hello + helo → hello |
| Paragraph | Remove duplicate paragraphs | Same paragraph kept once |
Deduplication Algorithms
Exact Dedup (Set-based):
Input: ["a", "b", "a", "c", "b"]
Set: {"a", "b", "c"}
Output: ["a", "b", "c"]
Preserve First Occurrence:
Input: ["c", "a", "b", "a", "c"]
Output: ["c", "a", "b"]
Why You Need Text Deduplication
- Data cleaning: Clean duplicate data from crawlers or APIs
- Log analysis: Remove duplicate log lines, keep unique entries
- SEO content: Detect and remove duplicate content
- Email lists: Clean duplicate email addresses
- Code review: Detect duplicate code lines
- Text editing: Clean duplicates from copy-paste
How to Use an Online Tool
Using DevToolkit Pro's Text Deduplication Tool:
- Paste or input text
- Choose dedup mode (exact/case-insensitive/fuzzy)
- Choose sort order (preserve order/alphabetical/frequency)
- Real-time preview of deduplicated result
- One-click copy deduplicated text
Deduplication in Code
JavaScript
// Exact dedup (preserve order)
function dedupeExact(text) {
const lines = text.split('\n');
const seen = new Set();
return lines.filter(line => {
if (seen.has(line)) return false;
seen.add(line);
return true;
}).join('\n');
}
// Case-insensitive dedup
function dedupeIgnoreCase(text) {
const lines = text.split('\n');
const seen = new Set();
return lines.filter(line => {
const lower = line.toLowerCase();
if (seen.has(lower)) return false;
seen.add(lower);
return true;
}).join('\n');
}
// Fuzzy dedup (similarity-based)
function dedupeFuzzy(text, threshold = 0.85) {
const lines = text.split('\n');
const unique = [];
for (const line of lines) {
if (!unique.some(u => similarity(u, line) >= threshold)) {
unique.push(line);
}
}
return unique.join('\n');
}
// Jaccard similarity
function similarity(a, b) {
const setA = new Set(a.split(''));
const setB = new Set(b.split(''));
const intersection = new Set([...setA].filter(x => setB.has(x)));
const union = new Set([...setA, ...setB]);
return intersection.size / union.size;
}
Python
from collections import OrderedDict
def dedupe_exact(text):
"""Exact dedup, preserve order"""
lines = text.split('\n')
return '\n'.join(OrderedDict.fromkeys(lines))
def dedupe_ignore_case(text):
"""Case-insensitive dedup"""
lines = text.split('\n')
seen = set()
result = []
for line in lines:
lower = line.lower()
if lower not in seen:
seen.add(lower)
result.append(line)
return '\n'.join(result)
def dedupe_fuzzy(text, threshold=0.85):
"""Fuzzy dedup"""
from difflib import SequenceMatcher
lines = text.split('\n')
unique = []
for line in lines:
if not any(SequenceMatcher(None, u, line).ratio() >= threshold for u in unique):
unique.append(line)
return '\n'.join(unique)
FAQ
Does Deduplication Change Order?
Default preserves original order (first occurrence). Options: alphabetical sort or frequency sort (most frequent first).
How to Handle Empty Lines?
Options: 1) Keep empty lines, only dedup non-empty; 2) Merge consecutive empty lines into one; 3) Remove all empty lines. Choose based on your needs.
How to Set Fuzzy Dedup Threshold?
Threshold 0-1, closer to 1 = stricter. Recommended: 0.85-0.9 (loose) for minor variants, 0.95+ (strict) for near-identical lines only.
This article is brought to you by DevToolkit Pro. More developer tools at the homepage.
相关文章
Online Word Counter: Count Words, Characters, Lines Instantly
Learn how to use an online word counter to count words, characters, lines, and paragraphs. Understand Chinese vs English counting differences and use cases in writing and development.
UUID Generator: Complete Guide to Unique Identifiers (UUID, NanoID, ULID)
Understand UUID v4, NanoID, and ULID fundamentals. Learn how to use an online UUID generator, and discover best practices for databases, distributed systems, and API tracking.
Online User-Agent Parser: Identify Browser and Device Information
Learn how to parse User-Agent strings. Understand browser, operating system, and device type identification methods, and applications in data analytics and compatibility testing.