Tech Study

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

 

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

C String Functions

C String Functions perform certain operations, It provides many useful string functions which can come into action. The <string.h> header …

Read more

Function Pointer in C++

Introduction : Function Pointer in C++ Pointers: In function pointer in c++ are the variables that consist of addresses of …

Read more