How to find duplicates in 2 columns not 1
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 duplicates across two columns means you care about the combination of values, not whether either column contains repeats by itself. In other words, you are looking for duplicate pairs such as (John, Doe) appearing more than once, not just duplicate first names or duplicate last names separately.
Think in Terms of a Composite Key
The clean way to reason about this problem is to treat the two columns together as one composite key.
Example data:
| first_name | last_name |
| John | Doe |
| Jane | Smith |
| John | Doe |
| John | Smith |
In this table:
- '
Johnis duplicated in the first column' - '
Doemay be duplicated in the second column' - only
(John, Doe)is a duplicate pair
That distinction is what the query or formula must express.
SQL Solution
In SQL, the standard pattern is GROUP BY on both columns followed by HAVING COUNT(*) > 1.
This returns only combinations that occur more than once.
If you want the original rows rather than the grouped summary, join the grouped result back to the table:
That gives you every duplicate record whose two-column combination repeats.
Pandas Solution
In pandas, the same idea works with duplicated and the subset parameter:
keep=False marks all rows in each duplicate group rather than leaving one row unmarked.
If you want the frequency per pair:
Spreadsheet Approach
In Excel or Google Sheets, create a helper column that combines the two values and then detect duplicates on that helper key.
Example helper formula:
After that, you can use conditional formatting or COUNTIF on the helper column:
This works, but use a separator that cannot appear naturally in the data, or choose a more robust formula if collisions are possible.
Null and Whitespace Considerations
Real datasets often fail duplicate checks because the values are not normalized.
Examples:
- '
Johnversusjohn' - '
DoeversusDOE' - null versus empty string
If duplicates should be case-insensitive or whitespace-insensitive, normalize first.
In SQL that may mean TRIM and LOWER. In pandas that may mean .str.strip().str.lower() before duplicate detection.
When Order Matters
Most two-column duplicate checks assume column meaning is fixed, such as (first_name, last_name). If your problem is more like pairs where order does not matter, such as (A, B) being treated the same as (B, A), you must normalize the pair first before grouping.
That is a different problem from ordinary duplicate detection.
Common Pitfalls
The biggest mistake is checking each column independently and assuming that tells you whether the pair is duplicated. It does not.
Another issue is forgetting null, whitespace, or case normalization. Two rows that are logically identical may not compare equal until cleaned.
Spreadsheet users often concatenate values without a separator, which can create collisions such as AB plus C looking the same as A plus BC.
Finally, do not forget whether you want one representative duplicate group or every row that participates in the duplicate.
Summary
- To find duplicates in two columns, treat the two values as one composite key.
- In SQL,
GROUP BY col1, col2 HAVING COUNT(*) > 1is the standard answer. - In pandas, use
duplicated(subset=[...], keep=False). - In spreadsheets, a helper key column is usually the simplest approach.
- Normalize whitespace, case, and null behavior before checking duplicates if the data is messy.
Related reading
- 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 the installed pandas version
- How to find which columns contain any NaN value in Pandas dataframe
- How to fit list of numpy array into LSTM Neural Network?
- How to fix ipykernel_launcher.py error unrecognized arguments in jupyter?
- How to fix ''jupyter'' is not recognized as an internal or external command, operable program or batch file when running Jupyter on Windows?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free 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.