# Set drop() Method in Scala

**Scala | Set drop() Method**: In this article, we will learn about drop() method of set collection in Scala. It is basically used to drop items from the set. Here, we will learn its syntax and working examples.

Submitted by Shivang Yadav, on November 24, 2020

The *drop()* method defined for the set collection in Scala is used to drop (delete) elements from the set. The *drop()* method deletes the first n elements from the given set and returns the rest.

**Syntax:**

drop(n): set

**Parameters:**

- n - The method accepts a single parameter
*n*, which is the count of numbers to be deleted from the set.

**Return value:**

The method returns a set that contains elements that remained in the set after dropping the first *n* elements.

**Points to Remember: **

- Drop method deletes first n elements of the set.
- The parameter can have any positive value.
- If the number of elements to be deleted is equal to or greater than the size of the set. An empty set is returned.

**Examples:**

Example to illustrate the working of the drop method.

**Example 1: **Deleting first n elements from a set of size l, l > n.

// Program to illustrate the working of // drop() method in Scala object MyClass { def main(args: Array[String]) { val mySet = Set(5, 1, 9, 4, 0, 7, 2) println("The initial set 'mySet' : " + mySet) // Deleting first 4 elemets from the // set using drop method val newSet = mySet.drop(4); println("The set after deleting '4' elements using drop method: " + newSet) } }

**Output:**

The initial set 'mySet' : HashSet(0, 5, 1, 9, 2, 7, 4) The set after deleting '4' elements using drop method: HashSet(2, 7, 4)

**Explanation:**

In the above code, we have created a set *mySet* with 7 elements and printed it using *println* method. After this, we have used to *drop()* method to delete the first 4 elements of the set and store the result in a new set *newSet* and then printed the result.

**Code 2: **A different way to print the values of the list.

// Program to illustrate the working of // drop() method in scala object MyClass { def main(args: Array[String]) { val mySet = Set(5, 1, 9, 4, 0, 7, 2) print("The initial set 'mySet' : ") for(x <- mySet) print(x + " ") // Deleting first 4 elemets from the // set using drop method val newSet = mySet.drop(4); print("\nThe set after deleting '4' elements using drop method : ") for(x <- newSet) print(x + " ") } }

**Output:**

The initial set 'mySet' : 0 5 1 9 2 7 4 The set after deleting '4' elements using drop method : 2 7 4

**Example 2: **Deleting first n elements from the list of size l, where l > n.

// Program to illustrate the working of // drop() method in Scala object MyClass { def main(args: Array[String]) { val mySet = Set(5, 1, 9, 4, 0, 7, 2) print("The initial set 'mySet' : " + mySet) // Deleting first 4 elemets from the // set using drop method val newSet = mySet.drop(10); println("\nThe set after deleting '10' elements using drop method : " + newSet) } }

**Output:**

The initial set 'mySet' : HashSet(0, 5, 1, 9, 2, 7, 4) The set after deleting '10' elements using drop method : HashSet()

**Explanation:**

In this above code, we created a set named *mySet* with elements 7 and printed it. Then we have deleted 10 values from it. The method does not throws an error, instead return an **empty set**.

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