Skip to content
CampusEduX

Remove Duplicates, Preserve Order

Mediumpython
Learn the Concept: Python Data Structures

Given a list of numbers that may contain duplicates, print a new list with duplicates removed, keeping the original order of first appearance.

Why not just use set()?

Ans:converting straight to a set removes duplicates but loses the original order, since sets are unordered. You need to track what's already been seen while keeping the original sequence.

Approach: loop through the numbers, keep a set of values seen so far, and only add a number to the result list the first time it appears.

Input: One line: space-separated whole numbers.

Output: One line: the numbers with duplicates removed, printed as a Python list (in order of first appearance).

Related Problems

Input (stdin)

Output

Run your code to see output here...