Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Generics in Swift Generic Functions, Parameters and Constraints Generic Functions

Ingo Ngoyama
Ingo Ngoyama
4,882 Points

My code works but it wont let me pass.

I have put in all the correct arguments and labels and my output is matches so why is my code not working

func duplicate<T>( _ item: inout T, _ numberOfTimes: inout Int) -> [ T ]
    var anArray = [item]
    var counter = 0
        counter += 1
    } while counter < numberOfTimes
    return anArray

var a = 1
var b = 3

var myArray = duplicate(&a, numberOfTimes: &b)

2 Answers

Jeff McDivitt
Jeff McDivitt
23,970 Points

Although there are multiple ways to complete this. Here is the way that I did. There are several things that you are including that do not need to be included

func duplicate<T>(item:T, numberOfTimes:Int) -> Array<T> {
    var newArray: [T] = []
    for _ in 0..<numberOfTimes {
    return newArray
duplicate(item: 1, numberOfTimes: 4)
Christoph Eck
Christoph Eck
Courses Plus Student 7,021 Points

Short and working

func duplicate<T>(item: T, numberOfTimes: Int) -> [T] {
    var ary = [T](repeating: item, count: numberOfTimes)
    return ary