Skip to content

S1Q3 · Merge and Remove Duplicates

⚡ Quick Reference

Function: merge_and_remove_duplicates(list1: list, list2: list) -> set

Core idea: merge both lists, convert to a set - sets automatically remove duplicates.

def merge_and_remove_duplicates(list1: list, list2: list) -> set:
    return set(list1 + list2)

Key rules: - list1 + list2 concatenates the two lists - set(...) removes all duplicates automatically - Order is not preserved in a set - that's expected


Problem Statement

Problem

Write a function merge_and_remove_duplicates(list1: list, list2: list) -> set that merges both lists and returns a set containing all unique elements from both.

Examples:

Input
list1 = [1, 2, 2]
list2 = [2, 1, 1]
Output
{1, 2}
Input
list1 = ["ant", "bat"]
list2 = ["bat", "cat"]
Output
{'ant', 'bat', 'cat'}

Note on the first example

The problem statement shows {1, 2, 1} as output, but a set cannot contain duplicates - {1, 2, 1} is not a valid set. The correct output is {1, 2}. This appears to be a typo in the question.


Understanding the problem

A set in Python is an unordered collection of unique elements. Converting any iterable to a set automatically drops all duplicates - which is exactly what this problem asks for.

Step Operation Result
Concatenate [1, 2, 2] + [2, 1, 1] [1, 2, 2, 2, 1, 1]
Remove duplicates set([1, 2, 2, 2, 1, 1]) {1, 2}

Key insight

Sets are defined by uniqueness. The moment you call set() on anything, every duplicate vanishes. You don't need to write any loop or comparison logic - the data structure does the work.


Tracing the examples

Example 1: list1 = [1, 2, 2], list2 = [2, 1, 1]

list1 + list2  →  [1, 2, 2, 2, 1, 1]
set(...)       →  {1, 2}

Example 2: list1 = ["ant", "bat"], list2 = ["bat", "cat"]

list1 + list2  →  ["ant", "bat", "bat", "cat"]
set(...)       →  {"ant", "bat", "cat"}

"bat" appeared twice - once from each list - but the set keeps only one copy.


Solution approaches

def merge_and_remove_duplicates(list1: list, list2: list) -> set:
    return set(list1 + list2)

Concatenate with +, wrap in set(). Clean and direct.

def merge_and_remove_duplicates(list1: list, list2: list) -> set:
    return set(list1) | set(list2)

Convert each list to a set first, then use the | (union) operator to combine them. The union of two sets contains all elements from both, with no duplicates. Slightly more explicit about the intent.

def merge_and_remove_duplicates(list1: list, list2: list) -> set:
    return set(list1).union(list2)

Same as union but using the method form. set.union() accepts any iterable as argument - no need to convert list2 to a set first.

def merge_and_remove_duplicates(list1: list, list2: list) -> set:
    result = set()
    for item in list1 + list2:
        result.add(item)
    return result

Manually add each element to a set. The set's uniqueness constraint handles duplicates silently - add() on an existing element is simply a no-op. More verbose but makes the mechanics visible.


Set operations - quick reference

Operation Symbol Method Meaning
Union A \| B A.union(B) All elements from both
Intersection A & B A.intersection(B) Only elements in both
Difference A - B A.difference(B) In A but not in B
Symmetric diff A ^ B A.symmetric_difference(B) In one but not both

For this problem, union is the right operation.


Key takeaways

01

set() removes duplicates instantly

Any time a problem says "unique elements" or "remove duplicates", set() is almost always the answer. No loops needed.

02

Three ways to merge into a set

set(l1 + l2), set(l1) | set(l2), and set(l1).union(l2) are all equivalent. Know all three.

03

Sets are unordered

A set has no guaranteed order. {1, 2} and {2, 1} are the same set. Don't rely on insertion order when working with sets.