Swift: Merge 2 integer arrays
Video: Swift: Merge 2 integer arrays by Taught by Celeste AI - AI Coding Coach
Watch full page →Swift: Merge 2 Integer Arrays
Merging two integer arrays in Swift is straightforward and can be done using the array concatenation operator. This approach creates a new array containing all elements from both input arrays in order. It is useful when you want to combine data from multiple sources into a single collection.
Code
// Define two integer arrays
let array1 = [1, 2, 3]
let array2 = [4, 5, 6]
// Merge arrays using the + operator
let mergedArray = array1 + array2
// Print the merged array
print(mergedArray) // Output: [1, 2, 3, 4, 5, 6]
Key Points
- Use the + operator to concatenate two arrays in Swift easily.
- The merged array contains elements from the first array followed by the second.
- The original arrays remain unchanged after merging.
- This method works efficiently for arrays of any size and type.