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.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. func checknumbers(_ input: [Int]) -> Bool {
  2. for (index, number) in input.enumerated() {
  3. let thirdIndex = index + 2
  4. let secondIndex = index + 1
  5.  
  6. if secondIndex < input.endIndex && number == input[secondIndex] && number == input[thirdIndex] {
  7. return true
  8. }
  9. }
  10. return false
  11. }
  12. print(checknumbers([10, 10, 30, 30, 10]))
  13. print(checknumbers([10, 10, 10, 20, 20, 20, 20]))
  14. print(checknumbers([10, 10, 10, 20, 20]))

Result

swift programming languages techstudy.org

     





Related Topics





Popular Posts