w3resource

Swift Array Programming Exercises: Check if two given arrays of integers have 0 as their first element

Swift Array Programming: Exercise-15 with Solution

Write a Swift program to check if two given arrays of integers have 0 as their first element.

Pictorial Presentation:

Swift Array Programming Exercises: Check if two given arrays of integers have 0 as their first element

Sample Solution:

Swift Code:

func start1(_ a: [Int], _ b: [Int]) -> Bool {
    var ctr = 0
    
    if a.first == 0
    {
        ctr += 1
    }
    
    if b.first == 0 
    {
        ctr += 1
    }
    
    if  ctr == 2
   {
        return true
   }
   else 
   {
        return false
    }
	
}

print(start1([0, 1, 2], [0, 1]))
print(start1([0, 1, 2], [1, 2]))
print(start1([0, 1], [0]))

Sample Output:

true
false
true

Swift Programming Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Swift program to check if a given array of integers contains 3 twice, or 5 twice.
Next: Write a Swift program to compute the sum of the values of two given array of integers and each length 2. Find the array which has the largest sum and return the first array if the sum of two given arrays are equal.

What is the difficulty level of this exercise?



Inviting useful, relevant, well-written and unique guest posts