
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Merge Two Integer Arrays in Swift Without Using Library Function
In Swift, we can merge two or more integer arrays without using the library function. So Swift provides an addition assignment(+=) operator to merge two integer arrays. Using this operator we will merge two arrays and assign the result into a new array.
Syntax
newArray += array
Here, newArray is the resultant array and array represents the array which we want to merge.
Algorithm
Step 1 ? Create a function that takes two arrays as an argument and returns a merged array.
Step 2 ? Inside the function create an empty array to store the resultant array.
Step 3 ? Merge the first array and resultant array using the += operator and store the result into a resultant array.
Step 4 ? Now merge the second array and the resultant array using the += operator and store the result into a resultant array.
Step 5 ? Return the resultant array.
Step 6 ? Create two arrays of the same data type.
Step 7 ? Call the created function and pass the arrays into it.
Step 8 ? Print the output.
Example
In the following Swift program, we will merge two integer arrays without using the library function. So for that, we will create two arrays and a function which will merge two arrays using the += operator and return the result into a new array.
import Foundation import Glibc func mergeArray(fArray: [Int], sArray: [Int]) -> [Int] { var resultantArray = [Int]() resultantArray += fArray resultantArray += sArray return resultantArray } let Array1 = [10, 22, 31, 34] let Array2 = [24, 35, 16, 5] let mergedArray = mergeArray(fArray: Array1, sArray: Array2) print("Merged Array is: \(mergedArray)")
Output
Merged Array is: [10, 22, 31, 34, 24, 35, 16, 5]
Conclusion
So this is how we can merge two integer arrays without using the library function. Here we use the += operator to merge two arrays at a time, so we merged the first array in the resultant array and, then the second array in the resultant array so the final resulting array contains all the elements from the first and second array.