How to count all different elements at each index of two 2d Vectors in scala?

47 Views Asked by At

e.g we have two 2d Vectors:

Vector(Vector(A, A, A), Vector(A, A, A))
Vector(Vector(A, B, B), Vector(A, B, A))

The following result should be:

Vector(2, 1)

Because at index 0 both Vectors differ by 2 elements and at index 1 both differ by 1 elements.

1

There are 1 best solutions below

1
Alextem On

Got the answer with:

val v1 = Vector(Vector(A, A, A), Vector(A, A, A))
val v2 = Vector(Vector(A, B, B), Vector(A, B, A))

val result = (0 until v1.size).map(x => v1(x).zip(v2(x)).count(x => x._1 != x._2))

result is:

Vector(2, 1)