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 trialAananya Vyas
20,157 Pointsconfused with the return type.
the question is:
Write a function named duplicate with a single generic type parameter T. The function takes two arguments, item of type T, and numberOfTimes of type Int, and returns an array of type T. The function simply creates an array containing the element duplicated by the number of times specified. For example, calling duplicate(item: 1, numberOfTimes: 4) returns [1, 1, 1, 1].
pl go through the code attached (cant understand how to define the return type.)
let arr : T
func duplicate<T>(item : inout T , numberOfTimes: Int) -> arr {
var i = 0
for i in numberOfTimes {
arr[i] = item
i++
}
}
print(arr)
duplicate(item: &2, numberOfTimes: 2)
8 Answers
Josh Byron
5,156 PointsHere's the code I used to complete this task. Hope it helps :)
func duplicate <T> (item : T,numberOfTimes: Int)-> [T]{
var arr : [T] = [T]()
for _ in 0...(numberOfTimes - 1){
arr.append(item)
}
return arr
}
jc laurent
6,351 Pointsas the very moment you put this -> in your function you'll need the return key if not it will be a simple function with no return key as
func anyfunc() {
}
Qasa Lee
18,916 Pointsimport Foundation
//Write a function named duplicate with a single generic type parameter T. The function takes two arguments, item of type T, and numberOfTimes of type Int, and returns an array of type T.
//The function simply creates an array containing the element duplicated by the number of times specified. For example, calling duplicate(item: 1, numberOfTimes: 4) returns [1, 1, 1, 1].
func duplicate<T>(item: T, numberOfTimes: Int) -> Array<T> {
var a: [T] = []
for _ in (1...numberOfTimes) {
a.append(item)
}
return a
}
//duplicate(item: 1, numberOfTimes: 4)
//duplicate(item: "Hi~", numberOfTimes: 3)
This may help, good luck!
carriebarnett
16,732 PointsAfter researching a bit, I found a link that had a code example that is very close to what we are working on here in the code challenge and it helped me figure it out. http://austinzheng.com/2015/09/29/swift-generics-pt-2/
Look under the Subtile "Generic Functions" and there is a code example.
// Given an item, return an array with that item repeated n times func duplicate<T>(item: T, numberOfTimes n: Int) -> [T] { var buffer : [T] = [] for _ in 0 ..< n { buffer.append(item) } return buffer }
Alphonso Sensley II
8,514 Pointsgreat find!
Yuqing Lu
7,032 Pointsfunc duplicate<T>(item: T, numberOfTimes: Int) -> [T] { return [T](repeating: item, count: numberOfTimes) }
it works for me.
Clinton Johnson
28,714 PointsAananya Vyas yea i agree with you and the example above just made it more confusing, Can someone explain it for us.
Daryl Shy
11,720 Pointsfunc duplicate<T>(item: T, numberOfTimes: Int) -> [T] {
var arr: [T] = []
for _ in 0..<numberOfTimes {
arr.append(item)
}
return arr
}
jc laurent
6,351 PointsAs a returning function you need to use the return key in your function it should looks like
func (a)->arr {
var i = 0 return(i) }
Aananya Vyas
20,157 Pointsstill isn't clear :/
Randell Pur
4,934 PointsRandell Pur
4,934 PointsCould you explain how you came to this answer? It worked for me but I am still confused on how this works.