Carol
Carol

Reputation: 5

remove one seq elements from another seq in scala

Is there any efficient way of removing elements present in Seq one from Seq two

val one = Seq("A", "B")
val two = Seq("A", "B", "C", "E", "F")
val out = two.map {
  for (n <-one){
    _.filterNot(_ == n)
  }
}

expected output Seq("C", "E", "F") , empty or null of one should also be handled But I'm getting error Cannot resolve overloaded method 'map'

can anyone suggest what am doing wrong here ?

Upvotes: 0

Views: 545

Answers (1)

Tim
Tim

Reputation: 27356

This is the simplest way to do this efficiently:

two.filterNot(one.toSet.contains)

This uses contains to see whether an item is in one or not. one is converted to a Set as this implements contains more efficiently that Seq which does a linear search.

Upvotes: 1

Related Questions