w3resource

Swift Array Programming Exercises: Test if an array of integers contains a 3 or a 5

Swift Array Programming: Exercise-11 with Solution

Write a Swift program to test if an array of integers contains a 3 or a 5.

Pictorial Presentation:

Swift Array Programming Exercises: Test if an array of integers contains a 3 or a 5

Sample Solution:

Swift Code:

func has35(_ a: [Int]) -> Bool {
    if a.contains(3) || a.contains(5)
    {
        return true
    } 
    else
    {
        return false
    }
}

print(has35([2, 5]))
print(has35([2, 3, 5]))
print(has35([2, 4, 7]))

Sample Output:

true
true
false

Swift Programming Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Swift program to create a new array of length 2 containing the first and last elements from a given array of integers. The given array length must be 1 or more.
Next: Write a Swift program to test if an array of integers does not contain a 3 or a 5.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.