Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

set
swift
removeall
communitycreator

What is set.removeAll() in Swift?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The removeAll() method of a Set instance removes all the elements present in the set.

Syntax

set.removeAll()
Swift set.removeAll() syntax

Parameters

This method doesn't take any parameter.

Return value

This method returns an empty set.

Code example

Let's look at the code below:

// create some sets
var names: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"]
var workers: Set = ["Alicia", "Bethany", "Diana"]
var numbers: Set = [1, 2, 3, 4, 5, 7, 8, 9, 10]

// empty all sets
names.removeAll()
workers.removeAll()
numbers.removeAll()

// print sets
print(names)
print(workers)
print(numbers)
Using the set.removeAll() method to remove all elements in a Set

Code explanation

  • Line 2 to 4: We create some sets.
  • Lines 7 to 9: We remove all elements present in a set.
  • Lines 12 to 14: We print empty sets.

RELATED TAGS

set
swift
removeall
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring