Write a Swift program to find the larger value of a given array of integers and set all the other elements with that value

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 newarray(_ arra: [Int]) -> [Int] {
    var new_arra = arra
 
    if new_arra.first! > new_arra.last!
     {
        new_arra[1] = new_arra.first!
        new_arra[2] = new_arra.first!
     } 
    else 
     {
        new_arra[0] = new_arra.last!
        new_arra[1] = new_arra.last!
     }
    return new_arra
}
print(newarray([1, 4, 6]))
print(newarray([2, 5, 9]))
print(newarray([3, 8, 8]))

Result

Write a Swift program to find the larger value of a given array of integers and set all the other elements with that value
Write a Swift program to find the larger value of a given array of integers and set all the other elements with that value

Leave a Comment