How to remove an element from a set in Scala

2 Answers

0 votes
val set = Set(1, 2, 3, 4)
val updatedSet = set - 2

println(updatedSet) // Output: Set(1, 3)
  



/*
run:
 
Set(1, 3, 4)
 
*/

 



answered Aug 6, 2025 by avibootz
0 votes
import scala.collection.mutable.Set
 
val mutableSet = Set(1, 2, 3, 4)
mutableSet -= 2
 
println(mutableSet) 
 
 
 
/*
run:
  
HashSet(1, 3, 4)
  
*/

 



answered Aug 6, 2025 by avibootz
edited Aug 6, 2025 by avibootz
...