Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Omar De Yamlor
10,981 PointsSwift loops
Hi all,
I was trying to play around the concept of loops with an example I made myself, and couldn't succeed in achieving the desired outcome : compare the two arrays and append a third array with the values in common, while the fourth array will store the remaining values.
var firstArray = [1, 2, 10]
let secondArray = [1, 2, 3, 4]
var thirdArray: = []
var fourthArray: = []
var i = 0
while i < secondArray.count {
for number in firstArray {
if number == secondArray[i] {
thirdArray.append(number);
firstArray.remove(at: i)
}
} i += 1
}
Thanks for any help :)
2 Answers
kaspar elmans
9,655 PointsHi Omarg,
Cool challenge! I looked at your code and noticed a few errors. Most of the logic was right but you just mist a few things:
- The third and the fourth arrays aren't set up properly.
- You forgot to append to the fourth array.
- The counter is listed outside of the for loop. So only when the loop finishes does the counter go up. And because we are still in the while loop the for loop runs again.
- If you remove an element from the array, then the index number of all the elements in the array can change. For example: the first loop iterates over the index '0' which holds the number 1. Then you remove this number from the array so the the index of '0' becomes 2 and the index of '1' becomes 10. So removing an item from the array isn't a good approach for this challenge.
- I had to add a second for loop to iterate over the second array to add all the remaining numbers to the fourth array.
note: it only works when the first and second array have the same amount of elements. Else the index goes out of bounds in the loop.
let firstArray = [1, 2, 10, 12]
let secondArray = [1, 2, 3, 4]
var thirdArray = [Int]()
var fourthArray = [Int]()
var i = 0
var o = 0
while i < secondArray.count {
for number in firstArray {
if number == secondArray[i] {
thirdArray.append(number)
} else {
fourthArray.append(number)
}
i += 1
}
for number in secondArray {
if number != firstArray[o] {
fourthArray.append(number)
}
o += 1
}
}```
Omar De Yamlor
10,981 PointsHi Kaspar,
Many thanks for your contribution ! It's helped me see more clearly into the flaws of my code.
However, playing around the values of the firstArray and secondArray (for example [1, 4, 2, 12] and [1, 2, 3, 4] respectively) in your proposed solution triggers a value redundancy in the fourthArray [4, 2, 12, 2, 3, 4] (here 4 repeating itself two times in the array :/)
Perhaps it's not possible to do what I intended to with this example ?
kaspar elmans
9,655 PointsHi Omar,
Glad that it helped you :) To bad that is still doesn't work.. If have tried a few things but I still haven't figured it out. This becomes a lot easier in the future when you'll learn some other concepts ;)