Remove Duplicates Preserving Order
Python data engineering interview problem. Difficulty: beginner. Pattern: Arrays. About 10 minutes. Free to practice.
Keep first occurrence of each value and drop later duplicates. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
When data comes from several sources, the same value often appears more than once. You want a list where each value appears only once, but you do not want to lose the original order. Write a function unique_preserve(nums) that takes a list and returns a new list with the duplicates removed. Each value should stay in the position where it first appeared. For [1, 2, 1, 3, 2] the answer is [1, 2, 3]. Keep the print lines in the editor.
Requirements
- Return a list with each value at most once.
- Keep the values in the order of their first appearance.
- Return [] when the input is empty.
- Do not change the list you were given. Build a new one.
Constraints
- The values can be compared with == and can be used in a set. Think of numbers or short strings.
- Duplicates may be next to each other or far apart.
Examples
Input: unique_preserve([1, 2, 1, 3, 2]) Output: [1, 2, 3] Later 1 and 2 are dropped.
Topics: lakebench, python, dedupe, order.
More Python interview questions · All interview problems · Learn data engineering
Remove Duplicates Preserving Order
Interview-style drill: Keep first occurrence of each value and drop later duplicates.
Implement `unique_preserve(nums: list) -> list` returning unique values in first-seen order. Example: `[1, 2, 1, 3, 2]` → `[1, 2, 3]`. Keep the harness.