Data & Feature Engineering

Deduplication & Near-Duplicate Detection

Identifying and removing exact and near duplicate documents, images, and web pages at scale using MinHash and LSH.

🟡 intermediate5 min readdata
Deduplication and Near Duplicate Detection removes repetitive content from training sets and search catalogs. Duplicate data causes data leakage between train and test splits, wastes vector database storage, and degrades LLM pre-training efficiency. Engineers use Exact Hashing (MD5, SHA-256) for exact matches, MinHash with Locality Sensitive Hashing (LSH) for near-duplicate text, and Perceptual Hashing (pHash) for images.

Why Deduplication Matters

In web crawling, search engines, and LLM pre-training datasets, duplicate content is everywhere:

IMPACTS OF UN-DEDUPLICATED DATA:
1. Data Leakage:      Exact training samples leak into validation and test sets!
2. Wasted Compute:    LLMs spend 30% of pre-training budget reading duplicate text!
3. Vector DB Waste:   Indexing 10 copies of the same article inflates RAM costs by 10x!

1. Exact Deduplication (Cryptographic Hashes)

For 100% exact bit-for-bit duplicate files:

Compute a cryptographic hash (for example MD5 or SHA-256) of each document file. Store hashes in a bloom filter or key value store. If a hash collision occurs, discard the duplicate document immediately.

Document A ──► [ SHA-256 ] ──► "a8f3b..."
Document B ──► [ SHA-256 ] ──► "a8f3b..." ──► Match Found! Discard Document B!

2. Near-Duplicate Text (MinHash + LSH)

For near-duplicate text (documents sharing 80% identical sentences):

┌──────────────────────────┬──────────────────────────┬──────────────────────────┐
│ STEP 1: SHINGLING        │ STEP 2: MINHASHING       │ STEP 3: LSH BUCKETING    │
├──────────────────────────┼──────────────────────────┼──────────────────────────┤
│ Convert document into    │ Compress character sets  │ Hash signatures into     │
│ sets of overlapping      │ into compact numerical   │ buckets so similar       │
│ k-character shingles.    │ signature vectors.       │ documents collide in the │
│                          │ Preserves Jaccard Sim!   │ same bucket in O(1) time!│
└──────────────────────────┴──────────────────────────┴──────────────────────────┘

Jaccard Similarity measures overlap between document shingle sets $A$ and $B$:

$$J(A, B) = \frac{|A \cap B|}{|A \cup B|}$$

Locality Sensitive Hashing (LSH) groups similar MinHash signatures into candidate buckets, finding near-duplicates across 100 million documents in minutes instead of days.

3. Near-Duplicate Images (Perceptual Hashing / pHash)

Standard cryptographic hashes change completely if an image is resized by 1 pixel.

Perceptual Hashing (pHash) converts images into frequency domain representations (using Discrete Cosine Transform):

Say this out loud

Deduplication removes exact and near duplicate content from datasets and catalogs. Exact duplicates are caught using SHA 256 cryptographic hashes. Near duplicate text is identified by estimating Jaccard similarity using MinHash and Locality Sensitive Hashing. Near duplicate images are detected using Perceptual Hashing and Hamming distance.

Followups to expect

  1. How did deduplication improve LLaMA and RefinedWeb dataset quality? Deduplication using MinHash LSH removed over 30% of web crawl text, speeding up training while increasing downstream benchmark evaluation scores.
  2. What is Semantic Deduplication using Vector Embeddings? Using dense vector cosine similarity (cosine sim $> 0.95$) to identify semantically identical sentences that express the exact same idea using different words.

Check yourself

Question 1 of 3

Why is near duplicate deduplication essential when building LLM pre training datasets?

More in Data & Feature Engineering

See all →
Feature Engineering Fundamentals4 minSQL Questions in ML Interviews5 minEncoding Categorical Variables4 min