Last active
November 14, 2016 15:24
-
-
Save tomdiggle/d7ddb54730e55045ec9dc2a3b20cbe44 to your computer and use it in GitHub Desktop.
Appends an element to an array n times
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tested in Swift 3 | |
mutating func appendN(element: Element, n: Int) { | |
guard n > 0 else { | |
return | |
} | |
for _ in 0..<n { | |
self.append(element) | |
} | |
} | |
// Usage | |
var array = ["Bob", "Steve", "Barry"] | |
array.appendN(element: "John", n: 2) | |
// ["Bob", "Steve", "Barry", "John", "John"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment