Array Slicing Part 1
Part of the Fundamentals section of Coddy's Swift journey — lesson 73 of 86.
Sometimes you don't need an entire array—just a portion of it. Swift makes it easy to extract a subset of elements using array slicing with range operators.
To get a slice, you use a range inside the subscript brackets. The most common approach uses the half-open range operator (..<), which includes the start index but excludes the end index:
let numbers = [10, 20, 30, 40, 50]
let slice = numbers[1..<4]
print(slice) // [20, 30, 40]Here, 1..<4 gives us elements at indices 1, 2, and 3—but not index 4. This is useful when you know exactly where you want to stop.
If you want to include the end index, use the closed range operator (...):
let letters = ["a", "b", "c", "d", "e"]
let slice = letters[1...3]
print(slice) // ["b", "c", "d"]One important detail: the result of slicing is an ArraySlice, not a regular Array. If you need a true array, wrap it with Array():
let original = [1, 2, 3, 4, 5]
let sliced = Array(original[0..<3])
print(sliced) // [1, 2, 3]Challenge
EasyWrite a function getMiddleElements that takes numbers, start, and end and returns a slice of the array from the start index up to (but not including) the end index.
Use the half-open range operator (..<) to extract the specified portion of the array and return it as a regular array.
Parameters:
numbers([Int]): The array to slicestart(Int): The starting index (inclusive)end(Int): The ending index (exclusive)
Returns: An array containing elements from the start index up to (but not including) the end index ([Int])
Example: If numbers is [10, 20, 30, 40, 50, 60], start is 1, and end is 4, the function should return [20, 30, 40].
Cheat sheet
Swift allows you to extract a portion of an array using array slicing with range operators.
Use the half-open range operator (..<) to slice from a start index up to (but not including) an end index:
let numbers = [10, 20, 30, 40, 50]
let slice = numbers[1..<4]
print(slice) // [20, 30, 40]Use the closed range operator (...) to include the end index:
let letters = ["a", "b", "c", "d", "e"]
let slice = letters[1...3]
print(slice) // ["b", "c", "d"]Array slicing returns an ArraySlice. To convert it to a regular Array, wrap it with Array():
let original = [1, 2, 3, 4, 5]
let sliced = Array(original[0..<3])
print(sliced) // [1, 2, 3]Try it yourself
func getMiddleElements(numbers: [Int], start: Int, end: Int) -> [Int] {
// Write code here
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorCompound AssignmentRecap - Simple MathComparison Operators7Basic IO
Print FunctionString InterpolationReadLine InputType ConversionRecap - Till 120Recap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesArgument LabelsRecap - Sigma FunctionRecap - Validation FunctionDefault Values13Iterating Over Sequences
Iterating Over ElementsThe Enumerated MethodIterating Over Strings P1Iterating Over Strings P22Variables
Let vs VarType AnnotationsNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary Operator8Bill Split Calculator
Welcome MessageGetting Input3Optionals
What Are OptionalsUnwrapping With If LetGuard LetNil Coalescing OperatorRecap - Safe Unwrapping9Loops
For-In LoopWhile LoopRepeat-While LoopBreakContinueRecap - FactorialRanges In LoopsNested LoopRecap - Dynamic Input