w3resource

Swift Array Programming Exercises: Test if an array of length four containing all their elements from two given array of integers


Write a Swift program to test if an array of length four containing all their elements from two given array (each length two) of integers,.

Pictorial Presentation:

Swift Array Programming Exercises: Test if an array of length four containing all their elements from two given array of integers

Sample Solution:

Swift Code:

func new_array(_ a: [Int], _ b: [Int]) -> [Int] {
    var new_array: [Int] = []
    new_array.append(contentsOf: a)
    new_array.append(contentsOf: b)
    
    return new_array
}

print(new_array([1, 2], [3, 4]))
print(new_array([5, 5], [7, 7]))
print(new_array([-1, 0], [1, 2]))

Sample Output:

[1, 2, 3, 4]
[5, 5, 7, 7]
[-1, 0, 1, 2]

Go to:


PREV : Write a Swift program to create an array of length 2 containing the middle two elements from a given array of integers and even length 2 or more.
NEXT : Write a Swift program to swap the first and last elements of a given array of integers. Return the modified array (length will be at least 1).

Swift Programming Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.