I'm trying to count the differences between two arrays where the order DOES matter. For example:
array_one = ["A", "B", "C"]
array_two = ["B", "C", "A"]
This would yield 3 differences because:
array_one[0] != array_two[0]
array_one[1] != array_two[1]
array_one[2] != array_two[2]
Another example:
array_one = ["Z", "X", "Y"]
array_two = ["Z", "W", "Y"]
This would yield 1 because:
array_one[0] == array_two[0]
array_one[1] != array_two[1]
array_one[2] == array_two[2]
Any suggestions greatly appreciated.
@davidhu2000 beat me to it, but as this approach is slightly different, it might still be worth mentioning:
how about this
Just because I like stuff like this, I benchmarked the solutions, just wanted to share in case anyone is interested (I just switched the array to a bigger one to get more meaningful results)
how about this
You can merge the two arrays using
zip
, and count the inside array where the two values are equal.Another way is the just iterate through both array and run a counter
Of course, both assume the two arrays are the same length.
(0...arr1.length)
is the same as0 to arr1.length-1
, the...
is exclusive.