Removing an element from a list in Scala

Hey there!

Welcome to ClearUrDoubt.com.

In this post, we will look at a Scala program to remove an element from a list.

We can use “take()” and “drop()” methods to achieve our objective.

take(n): This method return first n elements as a list

drop(n): This method drops the first n elements and returns the rest of the elements as a list.

Ex:

val l = List(1, 2, 3, 4, 5, 6)

l.take(3)      // List(1, 2, 3)

l.drop(3)    // List(4, 5, 6)

Let’s look at the program:

Output:

Happy learning.

Please leave a reply in case of any queries.

Leave a Reply

Your email address will not be published. Required fields are marked *