Question #3. Write a function that removes all occurrences of a given integer from an array of integers.
I’m trying to wrap my head around the use of the ’_item’ parameter - and how it isolates a specific repeating element in an array.
After re-reading the text, I didn’t see a clear explanation on how ’_item’ works. Any insight on this would be greatly helpful. Any reference you can provide to the text (page # and paragraph) would be even better to help elucidate a concept I may have glossed over.
Challenge Answer:
var numbersArray = [8, 7, 3, 4, 2, 18, 5, 8, 23, 8, 12, 1, 0]
func removing(_item:Int, from array: [Int]) -> [Int] {
var newArray: [Int] = []
for candidateItem in array {
if candidateItem != item {
newArray.append(candidateItem)
}
}
return newArray
}
removing(8, from: numbersArray)
// [7, 3, 4, 2, 18, 5, 23, 12, 1, 0]
You’re missing a space. It’s _ item
. This is declaring that the first argument of the function has no external name (looking this up… Chapter 6, page #101, bottom of the page).
Hi there, narrativium.
Thank you for the reply and for the syntax correction.
However, to clarify my question (and to pull focus back onto the title of this post), I understand that _ item
declares a parameter and is ready to receive an argument. The section you pinpointed in the text focuses on external naming (or external names). This concept gels for me.
What I’m trying to understand is how the parameter specifically focuses on one repeating element in the array. In the above case, it’s the number 8.
How does it know to pick out (recognize) that specific element, just by stating _ item
, and then continue to cycle through all the elements, picking out all the 8s rather than just one number 8?
How does this differ from remove(at:)
?
What’s the difference in the compiler instruction? Is the function doing all the work?
This may be arduous, but perhaps breaking down what’s happening in the function declaration would be helpful.
I misunderstood your question, apologies.
remove(at:)
, removes an element at a specific location. For example, numbersArray.remove(at: 8)
would remove the element at index 8 (i.e. the ninth element, i.e. the element 23
) of numbersArray.
removing(_: from:)
removes all the elements of the array which are equal in value to item
. So, removing(8, from: numbersArray)
should remove all the elements which are equal to the Int value 8.
The actual implementation you’ve provided doesn’t remove the 8s, though, so much as it constructs a new array out of all the elements which aren’t 8s. It creates an empty array, then iterates through the original array: if the element candidateItem
in the original array is different from item
, candidateItem
is appended to the new array; if candidateItem
is equal to item
then it will be omitted.
So, removing(8, from: numbersArray)
should remove all the elements which are equal to the Int
value 8
.
The actual implementation you’ve provided doesn’t remove the 8s, though, so much as it constructs a new array out of all the elements which aren’t 8s.
Thanks much! This makes a lot more sense. I guess the ‘trickery’ lies in constructing a brand new array – an array which removes all the elements equal to the Int
value 8.
This fact (brand new array) warrants some delineation in the Swift Apprentice publication. Perhaps, they can add it to the next edition.
FYI The brand new empty array creation syntax is actually mentioned in the chapter’s current version on page 127 of the book right over here:
An empty array can be created using the empty array literal []. Because the compiler isn’t able to infer a type from this, you need to use a type annotation to make the type explicit:
var subscribers: [String] = []
Please let me know if you have any other questions or issues regarding the whole thing.