Your list has 47 duplicate lines and you will never spot them by eye

2026-11-24来源 dev.toEnglish

Your list has 47 duplicate lines and you will never spot them by eye

You paste a column from a spreadsheet into an email tool. 2,000 addresses. It sends. A week later someone replies: "I got this three times."

Nobody typed those addresses three times. They came from three different signup forms, three exports, three copy-paste sessions over two years. Duplicates do not arrive all at once. They creep in one export at a time, and by the time the list is big enough to matter, it is far too big to check by eye.

Eye-checking fails in a specific way. You scan a list and your brain pattern-matches the shape of a line, not its exact characters. sarah@acme.com and Sarah@acme.com look identical at a glance. So do sarah@acme.com with a trailing space and sarah@acme.com without one. The duplicates that cost you are exactly the ones that look clean.

The fix is one data structure: a Set.

function dedupeLines(text, { ignoreCase = false, trim = true } = {}) { const seen = new Set(); const out = []; for (const raw of text.split(/\r?\n/)) { const line = trim ? raw.trim() : raw; const key = ignoreCase ? line.toLowerCase() : line; if (line === "") continue; // blank lines are not data if (!seen.has(key)) { seen.add(key); out.push(line); } } return out; } 

A JavaScript Set keeps insertion order, so the first copy survives and every later copy disappears, and the list comes back in the order it arrived. That order matters more than people think. If the list is a log, the first occurrence is the earliest event. If it is a keyword list, the first occurrence is where you put it on purpose. Sorting the list to find duplicates destroys that information, which is why "sort, then dedupe" is the wrong default for almost every real list.

The two options in that snippet do most of the real work. Case-insensitive matching catches Sarah@ vs sarah@, and trimming catches the trailing space that Excel loves to add when you copy a column. Without those two, a dedupe tool reports "0 duplicates found" on a list that is visibly full of them, and the user concludes the tool is broken. It is not broken. It was just comparing the wrong thing.

There are two less obvious modes worth knowing. "Only lines that appear once" deletes every line that has any duplicate anywhere, which is how you find the entries that exist in exactly one of two lists you pasted together. "Only repeated lines" does the reverse: it shows you just the duplicates, with a count next to each, so you can audit what you are about to delete before you delete it. Never clean a customer list without looking at the removed pile first. One of those "duplicates" is usually two different people sharing a generic inbox like info@company.com.

The same idea scales past text. A CSV file is one record per line, so deduplicating lines deduplicates rows. A keyword export from any SEO tool is one keyword per line. A list of URLs from a crawl is one URL per line. The shape is always the same: one item per line, some of them repeated, order worth keeping.

If you want to try it on a real list, this free tool does exactly the snippet above, in your browser, with the modes and the audit list included: Remove Duplicate Lines. Paste the list, pick keep-first or keep-last, and copy the clean result. Nothing is uploaded, which matters when the list is customer emails. And if the question is not "which lines repeat" but "what changed between these two versions", the sibling tool for that job is the Text Diff Checker.

The list you send tomorrow will still have duplicates. The difference is that now you will know, and you will have removed them before anyone gets the same email three times.

原文:https://www.a8q9.cn/en/article/your-list-has-47-duplicate-lines-and-you-will-never-spot-them-by-eye-6360ed.html

相关文章