The clear() method is used to remove all the elements from the Set.
fun main() {
val x = mutableSetOf("Mohan", "Kriti", "Salim")
x.clear()
println(x)
}
So, in the above code we have created a Set and initialised to the variable x.
val x = mutableSetOf("Mohan", "Kriti", "Salim")Below is how the values are positioned in the Set,
-Function1.png)
Next, we have used the clear() function that removes all the elements from the Set making the Set empty.
And we get an empty Set as output,
[]