How do I check if there are duplicates in a flat list?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Detecting duplicates in a flat list is a common validation step before inserts, exports, and analytics pipelines. The best method depends on what output you need: a simple yes or no, the duplicate values themselves, or full frequency counts. A robust solution also defines normalization rules so duplicate logic matches business meaning.
Pick the Output You Actually Need
Different goals require different implementations:
- Boolean duplicate existence.
- Set or list of repeated values.
- Count map for every value.
Starting with the wrong goal often leads to unnecessary complexity or missing diagnostics.
Fast Boolean Check with set
For hashable values, compare list length with set length.
This is concise and typically linear time.
Early-Exit Scan for Large Inputs
If duplicates often appear early, a one-pass seen-set scan can stop sooner.
This avoids full processing when early duplicate detection is enough.
Retrieve Duplicate Values and Counts
For diagnostics, use Counter and keep entries with frequency greater than one.
This output is useful for reports and cleanup dashboards.
Normalize Values Before Checking
Many duplicate rules are semantic, not literal. Case and whitespace normalization is common.
Without normalization, duplicate detection can miss obvious duplicates from user perspective.
Handling Non-Hashable Elements
Set-based methods fail for lists and dictionaries. Convert each item to a stable key first.
Your key function should reflect actual uniqueness policy.
Database Constraints Still Matter
Application duplicate checks are useful, but they do not replace database-level uniqueness constraints. Two requests can pass app validation simultaneously and still create duplicates without a unique index.
Treat app checks as early feedback and DB constraints as final integrity guard.
JavaScript Equivalent Pattern
The same concept applies in frontend or Node code.
For object lists, map each object to a comparable string or tuple-like key before set checks.
Performance Considerations
For list length n, set-based duplicate detection is generally O(n) time and O(n) memory. Nested-loop checks are O(n^2) and should be avoided for non-trivial data sizes.
If data streams continuously, maintain incremental seen state instead of re-scanning full history each time.
Common Pitfalls
- Using nested loops when set-based logic is available.
- Forgetting normalization rules for text-heavy data.
- Using set checks when business needs detailed duplicate counts.
- Applying set logic directly to unhashable item types.
- Relying only on application checks and skipping unique DB constraints.
Summary
- Duplicate detection strategy should match desired output shape.
- Set comparison is fastest for simple existence checks.
- '
Counteris best when counts and duplicate values are needed.' - Normalize values to align detection with business semantics.
- Pair application checks with database uniqueness guarantees.

