# Best way to compare 2 Arrays/Lists

Hi, I have two Arrays/Lists which I want to compare with each other. What is the best way to do this?

I.e. Array1 and List2, Does List2 have the same elements and number of elements as Array1? Does not matter in what order the elements are organized.

Currently I am looping through the first array/list and then nesting a loop through the other array/list… but this approach is kind of messy when facing issue of duplicates etc

1 Like

@DEATHFISH
give a try on
`arr1.Intersect(arr2).Count = arr1.length`
for a quick check
Let us know if you need a more detailed check e.g. cross checking more details from arr2

1 Like

@ppr How do you ignore case and trim whitespace when using this method?

Any help?

@DEATHFISH I guess with a bit of modification along with the Except Method we can land to this query :

``````if(Array1.Count=List2.Count,Array1.Select(Function(x)x.Trim.ToLower).ToArray.Except(List2.Select(Function(x)x.Trim.Tolower)).Count=0,False)
``````
1 Like

@DEATHFISH
As an alternate and introducing the the SequenceEqual Approach we can restrict more strict the check.

``````arr1.Select(Function (x) x.Trim.ToLower).OrderBy(Function (x) x).SequenceEqual(arr2.Select(Function (x) x.Trim.ToLower).OrderBy(Function (x) x))
``````

Working with the Set Operations (Intersect, Except) is very helpfully. In some cases it requires . This It can ommit the the-both-have-same-length-check
Compare_SequenceEqual.xaml (5.4 KB)

1 Like

@supermanPunch @ppr
How do you use this code to get the intersection result between two Arrays/Lists? (trim whitespace and ignore case)

E.g. Array 1: Item 1, Item 2, Item 3, Item 4
Array 2: Item 3, Item 4, Item 5, Item 6

Intersect: Item 3, Item 4

Versus just getting the boolean condition earlier

@DEATHFISH Check this Expression :

``````Array1.Select(Function(x)x.Trim.ToLower).ToArray.Intersect(List2.Select(Function(x)x.Trim.Tolower)).ToArray
``````