Reputation: 2243
I have two Lists
val l1 = List(1,2,3)
val l2 = List(1,3,3)
with
l1.diff(l2)
I can find the difference in the list; at the same time I am interested in index where the difference found also; can i know what is the solution in scala ?
Note : All the time both the list size is going to be same.
Upvotes: 1
Views: 464
Reputation: 12631
Another way which shows you very easy which list and where you can find the value:
l1.diff(l2).map(v => (v, l1.indexOf(v), l2.indexOf(v)))
// res6: List[(Int, Int, Int)] = List((2,1,-1))
Upvotes: -1
Reputation: 7162
You can just add indexes to both lists and diff then:
val diff = l1.zipWithIndex.diff(l2.zipWithIndex)
-> List((2,1)) // different value is 2 and index is 1
Upvotes: 7
Reputation: 5424
val indexes = (l1 zip l2 zipWithIndex).filter(x => x._1._1 != x._1._2).map(_._2)
val indexesWithDiffValues = (l1 zip l2 zipWithIndex).filter(x => x._1._1 != x._1._2)
this code will give you a list of indexes you want.
Upvotes: 2