Suppose I have two lists:
val a = List('a', 'b', 'c')
val b = List('a', 'b', 'c', 'd')
I want to get the element which is not in the first list (in this case it’s ‘d’). I know I can do this with a loop, but is there any fancy functional way to do this quickly in one line?
I’ve been looking at the Scala List API, but could only found union and intersection (which will give me List(‘a’, ‘b’, ‘c’, ‘d’) and List(‘a’, ‘b’, ‘c’) respectively)
I think you can use
b -- a. Here is the documentation from scala:Sorry for the deprecated method, here is the current good one:
list1 filterNot (list2 contains)