How to Remove Duplicates from a List in Every Programming Language

"Remove duplicates" means the same thing in every language: keep only one copy of each distinct value. The fast way to do it is also nearly identical everywhere - lean on a hash-based structure instead of comparing every item to every other one. This page is a quick reference across the languages people search for most; jump to yours, or read the general no-code guide if you'd rather not write anything.

Quick answer

The universal pattern is: build a set (or set-like structure) from your list, which automatically collapses duplicates, then convert it back to a list if you need one. This runs in roughly O(n). The catch in every language is the same too - plain sets usually don't preserve the original order, so if order matters, use an order-preserving variant (shown per language below) instead of a bare set conversion.

Python

items = ['apple', 'banana', 'apple', 'cherry', 'banana']

# Order not preserved
unique = list(set(items))

# Order preserved (Python 3.7+, dicts keep insertion order)
unique_ordered = list(dict.fromkeys(items))
print(unique_ordered)  # ['apple', 'banana', 'cherry']

dict.fromkeys() is the idiomatic order-preserving dedup in modern Python - it's faster and more readable than a manual loop with a seen-set. Full Python guide →

JavaScript

const items = ['apple', 'banana', 'apple', 'cherry', 'banana'];

const unique = [...new Set(items)];
console.log(unique); // ['apple', 'banana', 'cherry']

Unlike Python's plain set, JavaScript's Set preserves insertion order, so the spread-into-array pattern above is both fast and order-preserving in one step. Full JavaScript guide →

SQL

SELECT DISTINCT customer_id
FROM orders;

DISTINCT removes duplicate rows from a query result. Deleting duplicate rows that already exist in a table is a different problem, usually solved with a ROW_NUMBER() window function. Full SQL guide →

Java

List<String> items = new ArrayList<>(List.of("apple", "banana", "apple", "cherry", "banana"));

List<String> unique = new ArrayList<>(new LinkedHashSet<>(items));
System.out.println(unique); // [apple, banana, cherry]

LinkedHashSet is the order-preserving equivalent of HashSet - it dedupes exactly the same way but keeps insertion order, which a plain HashSet does not guarantee.

Bash

# Order not preserved, requires sorting
sort -u file.txt

# Order preserved
awk '!seen[$0]++' file.txt

sort -u is the fastest one-liner but reorders your data alphabetically as a side effect. awk '!seen[$0]++' tracks every line it has already printed and skips repeats, preserving the original order. Full Bash guide →

PowerShell

$items = @('apple', 'banana', 'apple', 'cherry', 'banana')

$unique = $items | Select-Object -Unique
$unique # apple, banana, cherry

Select-Object -Unique preserves order and is case-insensitive by default - worth knowing if your list contains values that differ only by case. Full PowerShell guide →

pandas (Python DataFrames)

df_unique = df.drop_duplicates()

# Only consider certain columns, keep the last occurrence
df_unique = df.drop_duplicates(subset=['email'], keep='last')

drop_duplicates() operates on whole rows by default, with subset and keep parameters to control which columns count and which occurrence survives. Full pandas guide →

Excel and Google Sheets

Excel's Data ribbon has a built-in Remove Duplicates command, and Google Sheets has both a UNIQUE() formula and a Data Cleanup menu option. Both tools compare stored values, not visual appearance, so rows that look identical can survive if they differ in an invisible way - trailing spaces, non-breaking spaces, or numbers stored as text are the usual culprits. Why Excel's Remove Duplicates sometimes fails → · Full Google Sheets guide →

No code needed - paste your list, duplicates removed instantly.

Open Remove Duplicate Lines →

Want the plain-language version, no code?

See the general guide to removing duplicates from any list.

Read the guide →

Frequently Asked Questions

What is the fastest way to remove duplicates from a list in any language?
Put the items into a hash-based structure that only allows unique values - a set in Python, a Set in JavaScript, a HashSet in Java - then convert it back to a list. This runs in roughly O(n) instead of the O(n squared) you get from comparing every item to every other item.
Does removing duplicates this way preserve the original order?
It depends on the method. Plain sets in most languages do not guarantee order. If order matters, use an order-preserving approach: Python's dict.fromkeys(), JavaScript's Set (which does preserve insertion order, unlike Python's plain set), or a manual loop with a seen-tracker like Bash's awk '!seen[$0]++'.
How do I remove duplicates case-insensitively?
Normalize each item to the same case (lowercase, typically) before deduplicating, but keep a way to recover the original casing if you need it in the output - for example building a dictionary keyed on the lowercased value where the value is the first original-cased item seen.
What if two items look identical but are not being deduplicated?
Almost always an invisible character difference - a trailing space, a non-breaking space instead of a regular one, or a different line ending. Paste the list into the Invisible Character Detector to see exactly what's different before deduplicating.
What's the difference between removing duplicates from one list versus across multiple lists?
Removing duplicates from one list means keeping only the first occurrence of each value within that single list. Working across multiple lists is a different operation - you're usually asking what's common between them or what's unique to one, which is a comparison, not a dedup. See Compare 2 Lists for that case.
What if I don't want to write any code at all?
Paste your list into the Remove Duplicate Lines tool and it returns a deduplicated list instantly in your browser, with options for case sensitivity and whitespace handling.

Share this tool

Featured on ToolDirs
© 2026 ListDiff

Free online list and text processing tools.