I'm trying to figure out a way to change the value of element in array to certain value in swift.
I want to replace the value with a different value as follows:
- 0 -> 0
- 1 -> 4
- 2 -> 3
- 3 -> 2
- 4 -> 1
- 5 -> 0
- 6 -> 1
- 7 -> 3
- 8 -> 2
- 9 -> 1
I have a group of 6 numbers, for example 123456, and I want to individually replace each digit with the corresponding digit as shown above.
I figured out what to do for the most part but it fails if there are 2 of the same digits. Can anyone tell me what I'm missing? For example, if my array is [1, 2, 3, 4, 5, 6] the new array will be [4, 1, 3, 0, 2, 4]. It fails in the example where the original array is [5, 5, 5, 5, 5, 5] because the new array is [0].
If anyone has an idea how to do this I would appreciate it.
Thank you!
What I have tried:
var elements = [1, 2, 3, 4, 5, 6]
for (i, digit) in elements.enumerated() {
if digit == 1 {
elements[i] = 4
} else if digit == 2 {
elements[i] = 3
} else if digit == 3 {
elements[i] = 2
} else if digit == 4 {
elements[i] = 1
} else if digit == 5 {
elements[i] = 0
} else if digit == 6 {
elements[i] = 4
} else if digit == 7 {
elements[i] = 3
} else if digit == 8 {
elements[i] = 2
} else if digit == 9 {
elements[i] = 1
} else if digit == 0 {
elements[i] = 0
}
}