Swift Set removeFirst()

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?