﻿ Swift Array Programming Exercise: Find the larger value of a given array of integers and set all the other elements with that value - w3resource

# Swift Array Programming Exercises: Find the larger value of a given array of integers and set all the other elements with that value

## Swift Array Programming: Exercise-7 with Solution

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

Pictorial Presentation:

Sample Solution:

Swift Code:

``````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, 2, 3]))
print(newarray([-5, -4, 0]))
print(newarray([12, 14, 16]))
```
```

Sample Output:

```[3, 3, 3]
[0, 0, 0]
[16, 16, 16]
```

Swift Programming Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

﻿

Inviting useful, relevant, well-written and unique guest posts