Govind Singh
Govind Singh

Reputation: 15490

How do I remove all List elements from an Array?

I have to remove all List elements from the Array.

scala> var numbers=Array("321","3232","2401","7777","666","555")
numbers: Array[String] = Array(321, 3232, 2401, 7777, 666, 555)

scala> var nums=List("321","3232","2401")
nums: List[String] = List(321, 3232, 2401)

Would filter be useful here?

Upvotes: 0

Views: 3072

Answers (2)

elm
elm

Reputation: 20405

Truly using diff leads to a neat and simple approach; some other, more verbose ways,

numbers filterNot { nums.contains(_) }

for ( n <- numbers if !nums.contains(n) ) yield n

Upvotes: 2

tkroman
tkroman

Reputation: 4798

You should use numbers.diff(nums) - as simple as that:

scala> var numbers = Array("321", "3232", "2401", "7777", "666", "555")
numbers: Array[String] = Array(321, 3232, 2401, 7777, 666, 555)

scala> var nums = List("321", "3232", "2401")
nums: List[String] = List(321, 3232, 2401)

scala> numbers diff nums
res0: Array[String] = Array(7777, 666, 555)

Upvotes: 10

Related Questions