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 removeFirst()
In this tutorial, we will learn about the Swift set removeFirst() method with the help of examples.
The removeFirst() method removes the first element from the set.
Example
var brands: Set = ["Dell", "Apple", "Razer"]
// removes and returns first element from brands
print(brands.removeFirst())
// Output: Apple
removeFirst() Syntax
The syntax of the set removeFirst() method is:
set.removeFirst()
Here, set is an object of the Set class.
removeFirst() Parameter
The removeFirst() method doesn't take any parameter.
removeFirst() Return Value
The removeFirst() returns the removed element from the set
Note: Since Sets are unordered collections, the random element is removed and returned.
Example: Swift Set removeFirst()
var country: Set = ["Nepal", "Greece", "Spain"]
// removes and returns first element from country
print(country.removeFirst())
// print the modified set
print(country)
Output
Spain ["Greece", "Nepal"]
Here, we have used the removeFirst() method to remove the first element from the country set.
After removing the first element, we have printed the modified set.
Did you find this article helpful?