var anArray = ["one", "two", "three","one"] // Mutable let anImmutableArray = ["four", "five"] // Immutable
anArray.first // a Swift solution. nil if empty anArray[0] // Classic solution. Error if empty
anArray.last // a Swift solution. nil if empty anArray[anArray.count-1] // Classic solution. Error if empty
var emptyArray:[String] = [] if emptyArray.isEmpty { print("Empty Array") } emptyArray.first // Returns nil emptyArray.last // Returns nil
if anArray.contains("three") { print("Array contains value") }
// Create a Set then build array from it. Set guarantees uniqueness var aSet:Set = [] anArray.forEach{aSet.insert($0)} // Could do this: aSet = Set(anArray) aSet.count anArray = Array(aSet) anArray.count
// Just like prior solution. Uses Set's union function // Also skip closure anArray = ["one", "two", "three","one"] aSet = Set(anArray) aSet.unionInPlace(anImmutableArray) aSet.count anArray = Array(aSet) anArray.count
anArray = ["one", "two", "three","one"] let sortedArray = anArray.sort() // Returns a new sorted array sortedArray anArray anArray.sortInPlace() // Update the array in place anArray
anArray = ["one", "two", "three","one"] anArray.indexOf("two") // indexOf() can also be used to search an array let aNumericArray = [1, 2, 10, 20, 100] let x = aNumericArray.indexOf({$0 > 10}) // Find the index of first value greater than 10 x