Last Updated: November 19, 2020
· anthonylevings

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

Related protips:

Installing Xcode Command Line Tools on OS X Mavericks