Compare 2 arrays contain (can be found in both array) and match (by same position)

String[] 1: "1,2,7,3,4"

String[] 2: "3,2,7,4,6"

how do i 
Part 1) first compare the two string[] contain the same element, regards position.  (2,7,3,4)

Part 2) second, how to identify 2.1) those are the same element but in same position(2,7) , 2.2) but not in the same position (3,4)?



can i use string.intersect?
Assuming i assigned Part 1) (2,7,3,4) as match for each element <-- indicating it can be found in two arrays.

then i  assigned the Part 1) (2,7,3,4)element contained in two strings [] but in the same position (2,7) as YES,  and not in the same position (3,4) as NO.


Where Match + Yes = contain and match (correct number in correct position)
and Match + No = contain but don't match ( correct number in wrong position)

Please advise.

@Hoong_Chun_Wong
Welcome to the forum and thank you for this nice question

Indeed arr1.Intersect(arr2).toArray would find the common values. But here the origin positions will be lost and would lead to e.g. many IndexOf calls. IndexOf would not work correctly once duplicates will occur.

LINQ and the Datatype Tuple will help us:

(From  a In arrValues1.Select(Function (s, index) Tuple.Create(index,s)).toList
Join b In arrValues2.Select(Function (s, index) Tuple.Create(index,s)).toList
On a.Item2 Equals b.Item2
Let check = (a.Item1 = b.Item1)
Select Tuple.Create(check,a.Item2)).toList

we do for each array a sequence of tuples with the value and the index information:

on both sequences a Join will bring the same Values together
with a check on the position info we can persist the compare result:
Let check = (a.Item1 = b.Item1)
finally a list of tuples with the position check result and value is returned

so with some common methods we can retrieve the info if all items have the same positions or not
Also the different lists can be filtered and retrieved:
grafik

Find demo XAML here:
2Arr_GetSameValue_SameIndex.xaml (6.3 KB)

Some Info on LINQ find here:
https://www.tutorialsteacher.com/linq/linq-tutorials

Let us know your feedback

1 Like

Hi Peter,

Thanks… works great…

the next step, i was trying to count and print the value in correct and wrong position.

But when i added .count, the count doesn’t seem right.

2Arr_GetSameValue_SameIndex + counting.xaml (9.8 KB)

@hcw
I am not sure if I got you in total as in my initial logging most was already done.
However find modified XAML here:
2Arr_GetSameValue_SameIndex.xaml (7.5 KB)

with this logging;
grafik

your requests should be showcased

Let us know your feedback

Hi Peter, that’s great…

but i found another “error”
image

Assuming 5772 is array 1, and 5727 is array 2
2 values with same position is correct, but 4 values of different position seems off. It should be 2,7 (2 counts?)

@hcw
i will have a look on it, the Join is working correctly but is different to our meaning / needs. Let me think about this

@hcw
give some plays on this:
2Arr_GetSameValue_SameIndex.xaml (7.7 KB)

it also has some minor risks to fail. To overcome we have to process more complex but it should be solvable as I had done similar in another case.

@Hoong_Chun_Wong @hcw
find reworked XAML here:
2Arr_Get_SameValue-SameIndex-Unmatched-Unbound.xaml (17.4 KB)

let us know your feedback