Home โ€บ Interview Questions โ€บ How can you remove duplicate values from a list?

How can you remove duplicate values from a list?

๐ŸŸข Easy Coding Junior level
1Times asked
Jul 2026Last seen
Jul 2026First seen

๐Ÿ’ก Model Answer

To remove duplicates while preserving order, you can use a set to track seen items and build a new list. Example:

python
def remove_duplicates(lst):
    seen = set()
    result = []
    for item in lst:
        if item not in seen:
            seen.add(item)
            result.append(item)
    return result

This runs in O(n) time and O(n) space. If order is not important, you can simply convert to a set: list(set(lst)), which is also O(n) but may reorder elements. For stable ordering with Python 3.7+, dict.fromkeys(lst) or list(dict.fromkeys(lst)) achieves the same effect with O(n) complexity.

This answer was generated by AI for study purposes. Use it as a starting point โ€” personalize it with your own experience.

๐ŸŽค Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI โ€” Starts at โ‚น500