Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
Swift Set shuffled()
In this tutorial, we will learn about the Swift set shuffled() method with the help of examples.
The shuffled() method shuffles all the elements of a set.
Example
var numbers: Set = [1, 2, 3, 4, 5]
// shuffled elements of numbers
var result = numbers.shuffled()
print(result)
// Output: [ 5, 1, 2, 4, 3 ]
shuffled() Syntax
The syntax of the set shuffled() method is:
set.shuffled()
Here, set is an object of the set class.
shuffled() Parameters
The shuffled() method doesn't take any parameters.
shuffled() Return Value
The shuffled() method returns a shuffled array.
Example: Swift set shuffled()
var languages: Set = ["Swift", "C", "Java"]
print("Original: ", languages)
// shuffle elements of languages
var result = languages.shuffled()
print("Shuffled: ", result)
Output
Original: ["C", "Java", "Swift"] Shuffled: ["Swift", "Java", "C"]
Here, we have used the shuffled() method to shuffle the elements of the languages set.
Did you find this article helpful?