w3resource

Swift Array Programming Exercises: Check if a given array of integers contains a 3 next to a 3 somewhere

Swift Array Programming: Exercise-27 with Solution

Write a Swift program to check if a given array of integers contains a 3 next to a 3 somewhere.

Pictorial Presentation:

Swift Array Programming Exercises: Check if a given array of integers contains a 3 next to a 3 somewhere

Sample Solution:

Swift Code:

func has33(array_nums: [Int]) -> Bool {
    for x in 0..<array_nums.count-1
    {
        if array_nums[x] == 3 && array_nums[x+1] == 3
        {
            return true
        }
    }
    return false
}
print(has33(array_nums: [1, 3, 3]))
print(has33(array_nums: [1, 3, 1, 3]))
print(has33(array_nums: [3, 1, 3, 3]))
 

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 compute the sum of the numbers of a given array of integers except the number immediately after a 11.
Next: Write a Swift program to test if the number of 1's is greater than the number of 3's of a given array of integers.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/swift-programming-exercises/array/swift-array-exercise-27.php