﻿ Swift Array Programming Exercise: Create a new array that is left shifted from a given array of integers - w3resource

# Swift Array Programming Exercises: Create a new array that is left shifted from a given array of integers

## Swift Array Programming: Exercise-38 with Solution

Write a Swift program to create a new array that is left shifted from a given array of integers. So [11, 15, 13, 10, 45, 20] will be [15, 13, 10, 45, 20, 11].

Sample Solution:

Swift Code:

``````func left_shift(array_nums: [Int]) -> [Int] {
var new_array = array_nums

for x in 1..<array_nums.count {
new_array[x-1] = array_nums[x]
}

new_array[new_array.count-1] = array_nums

return new_array
}

print(left_shift(array_nums: [11, 15, 13, 10, 45, 20]))
print(left_shift(array_nums: [0, 1, 2]))
print(left_shift(array_nums: [1, 2]))
print(left_shift(array_nums: ))
```
```

Sample Output:

```[15, 13, 10, 45, 20, 11]
[1, 2, 0]
[2, 1]

```

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