How to Remove Duplicate Lines
Deduplicating a list means keeping one copy of each distinct line. The difficulty is deciding what counts as the same line, because a trailing space or a capital letter can make two entries look identical on screen and different to a program.
Exact duplicates
An exact duplicate is a line that is character-for-character identical, including spaces and case. Those are safe to remove and are what most tools do by default.
Keeping the first occurrence preserves the original order, which is usually what you want. Keeping the last is the other option and changes the order of the remaining lines.
Paste the list to remove repeated lines.
Open the Remove Duplicate Lines →Near duplicates
A trailing space, a tab, a non-breaking space or a capital letter makes a line different. These are the entries that survive a deduplication and then look wrong in the result.
| Line A | Line B | Same? |
|---|---|---|
| apple | apple | No, trailing space |
| Apple | apple | No, different case |
| apple | apple | Yes, identical |
| apple | aple | No, different spelling |
Trim whitespace first, then decide whether case should be ignored. Both choices are defensible; the mistake is not making one deliberately.
Case and whitespace options
Trimming leading and trailing spaces is almost always right. Collapsing internal runs of spaces is usually right for pasted text.
Ignoring case is right for email lists and product names, and wrong for code, case-sensitive identifiers and anything where Apple and apple are different things.
Order of operations
If you also want the list sorted, sort first and then remove adjacent duplicates, which is how command-line tools do it and is fast on large lists.
If the original order matters, deduplicate first and sort a copy. Once sorted, the original order is gone.
Counting the result
Record the count before and after. If you expected to remove 12 duplicates and removed 40, either the list had more repeats than you thought or the comparison was too loose.
That check is the fastest way to catch an accidental case-insensitive pass on a list that needed case sensitivity.
Common mistakes
- Leaving trailing spaces so duplicates survive.
- Ignoring case when it matters, or respecting it when it does not.
- Sorting first and then being surprised the order changed.
- Removing duplicates from a list where repeats are meaningful, such as order lines.
- Deduplicating a list that contains hidden characters from a web page.
Large lists
For a list of a few thousand lines, any tool works. For hundreds of thousands, sort-based deduplication is much faster than comparing every line with every other line.
The browser tool handles typical lists instantly. For very large files, a command-line sort with unique is the practical option.
Duplicates that are not duplicates
Order lines, transaction logs and survey responses can legitimately repeat. Removing those lines destroys data rather than cleaning it.
Before removing anything, ask whether a repeat is an error or a fact. If it is a fact, the right fix is a count column, not deletion.
Keeping a record
Work on a copy, or keep the original in a second column. Deduplication is easy to run and hard to undo, especially if the original was pasted rather than saved.
If the list came from a file, keep the file. If it came from a message, save it before editing.
Related cleaning steps
After deduplication, trim whitespace, unify capitalisation and remove empty lines. Doing those first usually reduces the duplicate count substantially.
Empty lines are a common surprise: several blank lines are exact duplicates of each other, and removing them changes the line count by more than expected.
When repeats are meaningful
Order lines, attendance records and survey responses can legitimately contain the same text twice. Removing those lines destroys information rather than cleaning it.
If a repeat is a fact rather than an error, the fix is a count column, not deletion.
Things to check before you deduplicate
Decide whether case matters. "Apple" and "apple" are different lines to a strict comparison, so lowercase the list first if you want them merged.
Trailing spaces also create false differences. Trim whitespace before comparing, or two identical looking lines will both survive.
Keep a copy of the original list. Once duplicates are gone you cannot tell how many there were or where they sat.
Frequently asked questions
Does deduplication keep the first or last occurrence?
Most tools keep the first. Check yours if the order matters.
Will it remove lines that differ only in case?
Only if you choose a case-insensitive comparison.
Why are there still duplicates after removing them?
The lines probably differ by a trailing space, a hidden character or case.