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


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 countnumber(_ input: [Int]) -> Int {
  2. var ctr = 0
  3. for (index, number) in input.enumerated() {
  4. let nextIndex = index + 1
  5.  
  6. if nextIndex < input.endIndex && number == 4 && (input[nextIndex] == 4 )
  7. {
  8. ctr += 1
  9. }
  10. }
  11. return ctr
  12. }
  13. print(countnumber([4, 4, 2, 4, 4]))
  14. print(countnumber([4, 4, 3]))
  15. print(countnumber([4, 5, 2, 4]))
  16.  

Result

swift programming languages techstudy.org

     





Related Topics





Popular Posts