vogzvq
Last Updated: February 25, 2016
·
10.1K
· anthonylevings
Speechrocket4 square

Swift: Create an array of numbers 1 to 100 and divide into odds and evens using partition()

// create an empty Int array
var arr = [Int]()
// extend the array using a Range    
arr += 1...100
// partition based on the use of odd or even numbers
let idx = partition(&arr, indices(arr)) { i, _ in i%2==0 }
// use the returned point of partition (index) to split the array into odd and even
arr[0..<idx] // even
arr[idx..<arr.endIndex] // odd

Thanks to @AirspeedSwift for help with the partition algorithm

Say Thanks
Respond