How to pop the first element of an array in Swift

1 Answer

0 votes
import Foundation

var array = [1, 2, 3, 4, 5]

// Check if the array is not empty
if !array.isEmpty {
    // Remove the first element
    array.remove(at: 0)
}

// Print the updated array
print(array.map { "\($0)" }.joined(separator: " "))



/*
run:

2 3 4 5

*/

 



answered May 3, 2025 by avibootz
...