Day 19: Merge Two Lists

Objective

Your task today is to write a program that merges two lists into a single list. The merged list can either contain all elements (including duplicates) or only unique elements, depending on your chosen approach. This challenge will help you understand how to work with lists and combine data structures effectively.

For example:

  • Input: List 1: [1, 2, 3], List 2: [3, 4, 5]
  • Output: Merged List (with duplicates): [1, 2, 3, 3, 4, 5]
  • Output: Merged List (unique): [1, 2, 3, 4, 5]

Why This Challenge Is Important

Merging lists is a common task in programming, especially when working with data. It’s useful in:

  1. Data Integration: Combining data from multiple sources.
  2. Set Operations: Performing unions or intersections of data.
  3. Real-World Applications: For example, merging customer lists or combining search results.

Steps to Solve

1. Understand the Problem

  • You’ll be given two separate lists as input.
  • Combine them into a single list.
  • Decide if the merged list should include duplicates or only unique elements.

2. Approaches to Solve

  1. Basic Merge: Combine the two lists using simple concatenation.
  2. Unique Merge: Remove duplicates by using a set or filtering out repeated elements.

3. Input Format

  • Prompt the user to enter two lists of numbers, separated by spaces.
  • Convert the input into two lists of integers.

Code Examples

Python Example

Merging with Duplicates:

# Get input from the user
list1 = input("Enter the first list of numbers separated by spaces: ").split()
list2 = input("Enter the second list of numbers separated by spaces: ").split()

# Convert input to integers
list1 = [int(num) for num in list1]
list2 = [int(num) for num in list2]

# Merge the lists
merged_list = list1 + list2

# Output the result
print("Merged list (with duplicates):", merged_list)

Merging Without Duplicates:

# Get input from the user
list1 = input("Enter the first list of numbers separated by spaces: ").split()
list2 = input("Enter the second list of numbers separated by spaces: ").split()

# Convert input to integers
list1 = [int(num) for num in list1]
list2 = [int(num) for num in list2]

# Merge the lists and remove duplicates
merged_list = list(set(list1 + list2))

# Output the result
print("Merged list (unique):", merged_list)

Java Example

Merging with Duplicates:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Get input from the user
        System.out.print("Enter the first list of numbers separated by spaces: ");
        String[] input1 = scanner.nextLine().split(" ");
        System.out.print("Enter the second list of numbers separated by spaces: ");
        String[] input2 = scanner.nextLine().split(" ");

        // Convert input to ArrayLists of integers
        ArrayList<Integer> list1 = new ArrayList<>();
        for (String s : input1) {
            list1.add(Integer.parseInt(s));
        }
        ArrayList<Integer> list2 = new ArrayList<>();
        for (String s : input2) {
            list2.add(Integer.parseInt(s));
        }

        // Merge the lists
        ArrayList<Integer> mergedList = new ArrayList<>(list1);
        mergedList.addAll(list2);

        // Output the result
        System.out.println("Merged list (with duplicates): " + mergedList);
    }
}

Merging Without Duplicates:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Get input from the user
        System.out.print("Enter the first list of numbers separated by spaces: ");
        String[] input1 = scanner.nextLine().split(" ");
        System.out.print("Enter the second list of numbers separated by spaces: ");
        String[] input2 = scanner.nextLine().split(" ");

        // Convert input to HashSet of integers to remove duplicates
        HashSet<Integer> mergedSet = new HashSet<>();
        for (String s : input1) {
            mergedSet.add(Integer.parseInt(s));
        }
        for (String s : input2) {
            mergedSet.add(Integer.parseInt(s));
        }

        // Output the result
        System.out.println("Merged list (unique): " + mergedSet);
    }
}

JavaScript Example

Merging with Duplicates:

// Get input from the user
let list1 = prompt("Enter the first list of numbers separated by spaces:").split(" ").map(Number);
let list2 = prompt("Enter the second list of numbers separated by spaces:").split(" ").map(Number);

// Merge the lists
let mergedList = list1.concat(list2);

console.log("Merged list (with duplicates):", mergedList);

Merging Without Duplicates:

// Get input from the user
let list1 = prompt("Enter the first list of numbers separated by spaces:").split(" ").map(Number);
let list2 = prompt("Enter the second list of numbers separated by spaces:").split(" ").map(Number);

// Merge the lists and remove duplicates using a Set
let mergedList = [...new Set([...list1, ...list2])];

console.log("Merged list (unique):", mergedList);

Edge Cases to Consider

  1. Empty Lists: Handle cases where one or both lists are empty.
  2. All Duplicates: Ensure duplicate handling works if all elements are the same.
  3. Mixed Data Types: If allowed, ensure the program works with strings and numbers.
  4. Order Preservation: If merging without duplicates, consider whether to preserve the order of elements.

Extensions to Explore

  1. Intersection: Find elements that are present in both lists.
  2. Difference: Identify elements that are in one list but not the other.
  3. Custom Sorting: Allow the user to sort the merged list in ascending or descending order.

What You’ve Learned

  • How to merge two lists in different ways.
  • The use of sets for removing duplicates.
  • Practical applications of list operations in programming.

Next Steps

In Day 20: Simple Dictionary, you create a simple dictionary that stores key-value pairs. A dictionary is one of the most powerful data structures in programming, allowing you to store and access data efficiently by using keys instead of indexes.