Swift Cheatsheet
48 essential methods for coding interviews
Showing 48 of 48 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
append | array.append(element) | Add an element to the end of an array | O(1) amortized | essential |
remove | array.remove(at: index) | Remove and return element at index | O(n) | essential |
removeLast / popLast | array.removeLast() / array.popLast() | Remove and return last element (popLast returns optional) | O(1) | essential |
first / last | array.first / array.last | Access first or last element as optional | O(1) | essential |
count / isEmpty | array.count / array.isEmpty | Get element count or check if empty | O(1) | essential |
contains | array.contains(element) | Check if array contains an element | O(n) | essential |
subscript range | array[start..<end] | Get a slice of the array | O(1) for slice | essential |
String to Array | Array(string) | Convert string to array of characters | O(n) | essential |
String from Array | String(charArray) | Convert character array back to string | O(n) | essential |
count (String) | string.count | Get the number of characters | O(n) | essential |
lowercased / uppercased | string.lowercased() / string.uppercased() | Convert to lowercase or uppercase | O(n) | essential |
split | string.split(separator: Character) | Split string into array of substrings | O(n) | essential |
Character properties | char.isLetter / char.isNumber / char.isWhitespace | Check character type properties | O(1) | essential |
Dictionary literal | var dict: [Key: Value] = [:] | Create an empty dictionary | O(1) | essential |
Dictionary subscript | dict[key] | Get or set value for key (returns optional) | O(1) average | essential |
Dictionary default value | dict[key, default: value] | Get value with default, enables in-place mutation | O(1) average | essential |
keys / values | dict.keys / dict.values | Get collection of keys or values | O(1) for view | essential |
Set literal | var set: Set<Type> = [] | Create an empty set | O(1) | essential |
Set from Array | Set(array) | Create set from array (removes duplicates) | O(n) | essential |
insert (Set) | set.insert(element) | Add element to set, returns insertion result | O(1) average | essential |
contains (Set) | set.contains(element) | Check if element exists in set | O(1) average | essential |
sorted | array.sorted() | Return a sorted copy of the array | O(n log n) | essential |
sorted with closure | array.sorted { $0 > $1 } | Sort with custom comparator | O(n log n) | essential |
min / max | array.min() / array.max() | Get minimum or maximum element | O(n) | essential |
map | array.map { transform($0) } | Transform each element | O(n) | essential |
filter | array.filter { predicate($0) } | Keep elements matching predicate | O(n) | essential |
reduce | array.reduce(initial) { acc, elem in ... } | Combine elements into single value | O(n) | essential |
enumerated | array.enumerated() | Get sequence of (index, element) pairs | O(1) | essential |
if let / guard let | if let value = optional { } / guard let value = optional else { return } | Safely unwrap optional values | O(1) | essential |
Nil coalescing (??) | optional ?? defaultValue | Provide default value for nil | O(1) | essential |
Optional chaining | optional?.property | Safely access properties of optional | O(1) | essential |
abs | abs(number) | Get absolute value | O(1) | essential |
Int.max / Int.min | Int.max / Int.min | Maximum and minimum Int values | O(1) | essential |
insert | array.insert(element, at: index) | Insert an element at a specific index | O(n) | common |
firstIndex | array.firstIndex(of: element) | Find index of first occurrence | O(n) | common |
reversed | array.reversed() / array.reverse() | Return reversed view or reverse in place | O(1) for view, O(n) to materialize | common |
swapAt | array.swapAt(i, j) | Swap elements at two indices | O(1) | common |
hasPrefix / hasSuffix | string.hasPrefix(prefix) / string.hasSuffix(suffix) | Check if string starts/ends with substring | O(k) where k is prefix/suffix length | common |
joined | array.joined(separator: String) | Join array elements into a string | O(n) | common |
Character.asciiValue | char.asciiValue | Get ASCII value of a character | O(1) | common |
removeValue | dict.removeValue(forKey: key) | Remove and return value for key | O(1) average | common |
union / intersection | set1.union(set2) / set1.intersection(set2) | Combine sets or find common elements | O(m + n) for union, O(min(m,n)) for intersection | common |
flatMap | array.flatMap { transform($0) } | Map and flatten nested arrays | O(n * m) | common |
compactMap | array.compactMap { transform($0) } | Map and remove nil values | O(n) | common |
zip | zip(sequence1, sequence2) | Combine two sequences into pairs | O(1) | common |
first(where:) | array.first { predicate($0) } | Find first element matching predicate | O(n) | common |
stride | stride(from: start, to: end, by: step) | Generate sequence with custom step | O(1) to create | common |
subtracting | set1.subtracting(set2) | Return set with elements in set1 but not set2 | O(n) | useful |