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:
[1, 2, 3]
, List 2: [3, 4, 5]
[1, 2, 3, 3, 4, 5]
[1, 2, 3, 4, 5]
Merging lists is a common task in programming, especially when working with data. It’s useful in:
# 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)
# 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)
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);
}
}
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);
}
}
// 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);
// 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);
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.