In Swift, you can shuffle an array using the Fisher-Yates shuffle algorithm.
In this example,
intArray
containing integers.intArray
.import Foundation
var intArray = [1, 2, 3, 4, 5]
intArray.shuffle()
print("Shuffled Array:", intArray)
Shuffled Array: [3, 5, 2, 4, 1]
In this example,
strArray
containing strings.strArray
.import Foundation
var strArray = ["apple", "banana", "cherry"]
strArray.shuffle()
print("Shuffled Array:", strArray)
Shuffled Array: [cherry, banana, apple]
In this tutorial, we learned How to Shuffle an Array in Swift language with well detailed examples.