# How to delete elements from the Set in Scala?

Here, we are going to learn **how to delete elements from Set in Scala with example programs?**

Submitted by Shivang Yadav, on December 03, 2019

## Scala Set

In Scala, a Set is a collection of elements of the same type. All elements of the set are unique i.e. no elements are allowed. Sets can be mutable as well as immutable.

**Example:**

Set(1, 4, 5, 7, 12, 87, 213)

In Scala, you can remove elements from mutable as well as immutable sets. This operation is handled differently for both mutable as well as immutable sets.

### 1) Deleting elements from the Mutable Set

For deleting elements of a mutable set, we will use -= ,--=, retain, clear, remove.

-= |
Deletes single element from set. |

--= |
Deletes multiple element from set. |

retain |
Deletes multiple element based on a certain condition. |

clear |
Deletes all elements of the set. |

remove |
Removes the specified element and return boolean value of operation done. |

**Example 1: use of -= and --= methods **

object MyClass { def main(args: Array[String]) { val set = scala.collection.mutable.Set(2, 56, 577,12 , 46, 19, 90 , 32, 75, 81) println("The set is "+set) set -= 2; println("After deletion of one element, the set is "+set) set --= List(577, 12, 19); println("After deletion of multiple elements, the set is "+set) } }

**Output**

The set is HashSet(32, 81, 577, 2, 19, 56, 90, 75, 12, 46) After deletion of one element, the set is HashSet(32, 81, 577, 19, 56, 90, 75, 12, 46) After deletion of multiple elements, the set is HashSet(32, 81, 56, 90, 75, 46)

**Example 2:**

object MyClass { def main(args: Array[String]) { val set = scala.collection.mutable.Set(2, 56, 577,12 , 46, 19, 90 , 32, 75, 81); println("The set is "+set) set.retain(_ >20); println("After deletion using retain, the set is "+set) set.remove(577) println("After deletion using remove, the set is "+set) set.clear() println("After deletion using clear, the set is "+set) } }

**Output**

The set is HashSet(32, 81, 577, 2, 19, 56, 90, 75, 12, 46) After deletion using retain, the set is HashSet(32, 81, 577, 56, 90, 75, 46) After deletion using remove, the set is HashSet(32, 81, 56, 90, 75, 46) After deletion using clear, the set is HashSet()

### 2) Deleting elements from the Immutable Sets

Element in an immutable set cannot be changed. So, for performing deletion operation on these types of the set we need to create a new copy for every operation. -- and - operations are valid.

**Example:**

object MyClass { def main(args: Array[String]) { val set = scala.collection.mutable.Set(2, 56, 577,12 , 46, 19, 90 , 32, 75, 81); println("The set is "+set) var set1 = set - 2; println("After deletion of one element, the set is "+set1) var set2 = set1 -- List(577, 12, 19); println("After deletion of multiple elements, the set is "+set2) var set3 = set2 - (81, 46) println("After deletion of multiple elements, the set is "+set2) var set4 = set3 -- Array(56, 90); println("After deletion of multiple elements, the set is "+set2) } }

**Output**

The set is HashSet(32, 81, 577, 2, 19, 56, 90, 75, 12, 46) After deletion of one element, the set is HashSet(32, 81, 577, 19, 56, 90, 75, 12, 46) After deletion of multiple elements, the set is HashSet(32, 81, 56, 90, 75, 46) After deletion of multiple elements, the set is HashSet(32, 81, 56, 90, 75, 46) After deletion of multiple elements, the set is HashSet(32, 81, 56, 90, 75, 46)

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.