Write a Swift program to test whether a value presents sequentially three times in an array of integers or not

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 checknumbers(_ input: [Int]) -> Bool {
        for (index, number) in input.enumerated() {
        let thirdIndex = index + 2
        let secondIndex = index + 1
 
        if secondIndex < input.endIndex && number == input[secondIndex] && number == input[thirdIndex] {
            return true
        }
    }
    return false
}
print(checknumbers([10, 10, 30, 30, 10]))
print(checknumbers([10, 10, 10, 20, 20, 20, 20]))
print(checknumbers([10, 10, 10, 20, 20]))

Result

Write a Swift program to test whether a value presents sequentially three times in an array of integers or not
Write a Swift program to test whether a value presents sequentially three times in an array of integers or not

 

Leave a Comment