Removing Duplicate Elements While Preserving Order in Python

Question
Below is a clear, beginner-friendly explanation with complete Python implementations for Options A, B, C, and D, all of which:
  • Preserve the original order

  • Work with positive, negative, and zero values

  • Do NOT use <code>set()</code> or <code>dict.fromkeys()</code>

  • Manually handle duplicate detection

Problem Recap

Input:

[1, 2, 2, 3, 1]

Output:

[1, 2, 3]

We keep the first appearance only and ignore later duplicates.

✅ Option A: Loop + Extra List (Seen Values)

Idea

  • Maintain a <code>seen</code> list to remember numbers already encountered

  • Loop through the input list

  • If a number is not in <code>seen</code>, add it to both <code>seen</code> and <code>result</code>

Why it works

  • Order is preserved

  • Easy to understand

  • Time complexity: O(n²) (because <code>in</code> on a list is linear)

Code

def unique_elements_option_a(lst):
    seen = []
    result = []
    for num in lst:
        if num not in seen:
            seen.append(num)
            result.append(num)
    return result

✅ Option B: Nested Loop (No Extra Data Structure)

Idea

  • For each element, check all previous elements

  • If it appeared before, skip it

  • Otherwise, add it to the result

Why it works

  • Uses only loops

  • No extra memory for tracking duplicates

  • Time complexity: O(n²)

Code

def unique_elements_option_b(lst):
    result = []
    for i in range(len(lst)):
        duplicate = False
        for j in range(i):
            if lst[i] == lst[j]:
                duplicate = True
                break
        if not duplicate:
            result.append(lst[i])
    return result

✅ Option C: Using Index Comparison (<code>list.index()</code>)

Idea

  • <code>lst.index(x)</code> gives the first occurrence of <code>x</code>

  • If the current index equals the first index → keep it

  • Otherwise → duplicate

Why it works

  • Very readable

  • Still avoids <code>set()</code> and <code>dict</code>

  • Time complexity: O(n²)

Code

def unique_elements_option_c(lst):
    result = []
    for i in range(len(lst)):
        if lst.index(lst[i]) == i:
            result.append(lst[i])
    return result

✅ Option D: Build Result List & Check Against It

Idea

  • Build the result list gradually

  • Only add a number if it’s not already in the result

Why it works

  • Simple and clean

  • Preserves order naturally

  • Time complexity: O(n²)

Code

def unique_elements_option_d(lst):
    result = []
    for num in lst:
        if num not in result:
            result.append(num)
    return result

🔍 Example Usage (Same for All Options)

data = [1, 2, 2, 3, 1]
print(unique_elements_option_a(data))
print(unique_elements_option_b(data))
print(unique_elements_option_c(data))
print(unique_elements_option_d(data))

Output:

[1, 2, 3]

🧠 Summary Table

OptionExtra StructureComplexityBeginner Friendly
AYes (<code>seen</code>)O(n²)⭐⭐⭐⭐
BNoO(n²)⭐⭐⭐
CNoO(n²)⭐⭐⭐⭐
DYes (<code>result</code>)O(n²)⭐⭐⭐⭐⭐


Details
Purchase An Answer Below

Have a similar question?