I want to write an extension which allows me to put in an array and return a set number of elements from that array with no repeated items. How would I do that? This is what I have so far but it is not perfect. It does not consider duplicates and it does not seem like the best way for this to be done. I was thinking it might make sense to use a set for duplicates.
extension Array {
func randomElement(numberOfItems:Int) -> [Element] {
var finalReturn = Array()
for i in 0..<numberOfItems {
finalReturn.append(self[Int(arc4random_uniform(UInt32(self.count)))])
}
return finalReturn
}
}
usage should be like this.
let selected = allData.randomElement(numberOfItems: 10)