Write a Swift program to rotate the elements of an array of integers to left direction

Write a Swift program to count the number of times that two 4’s are next to each other in a given array of integers

Introduction

I have used Swift for windows 1.9 for debugging purpose. But you can use any Swift programming language compiler as per your availability.

func countnumber(_ input: [Int]) -> Int {
    var ctr = 0
        for (index, number) in input.enumerated() {
        let nextIndex = index + 1
 
        if nextIndex < input.endIndex && number == 4 && (input[nextIndex] == 4 ) 
        {
            ctr += 1
        }
    }
    return ctr
}
print(countnumber([4, 4, 2, 4, 4]))
print(countnumber([4, 4, 3]))
print(countnumber([4, 5, 2, 4]))
func countnumber(_ input: [Int]) -> Int {
    var ctr = 0
        for (index, number) in input.enumerated() {
        let nextIndex = index + 1
 
        if nextIndex < input.endIndex && number == 4 && (input[nextIndex] == 4 ) 
        {
            ctr += 1
        }
    }
    return ctr
}
print(countnumber([4, 4, 2, 4, 4]))
print(countnumber([4, 4, 3]))
print(countnumber([4, 5, 2, 4]))

Result

Write a Swift program to count the number of times that two 4's are next to each other in a given array of integers
Write a Swift program to count the number of times that two 4’s are next to each other in a given array of integers

 

Add a Comment

Your email address will not be published. Required fields are marked *