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:
1 2 3 4 5 6 7 8 9 10 |
package com.clearurdoubt object ListImpl extends App { val l = List(1,2,3,4,5,6,7) def removedAt(n: Int, list: List[Int]): List[Int] = (l take n) ::: (l drop n + 1) println(removedAt(3, l)) } |
Output:
Happy learning.
Please leave a reply in case of any queries.