Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I compare values in two lists?

Tags:

c#

.net

linq

I have two lists

List 01 => { A, B, C, D, E }
List 02 => { F, F, F, F, E }

I need to check if one element of List 02 exists in List 01, so the following should be false.

List 01 => { A, B, C, D, E }
List 02 => { F, F, F, F, F } // no element matches

And here it should be true.

List 01 => { A, B, C, D, E }
List 02 => { F, F, F, F, B } // last element matches

How can I check that?

I'm concerned about performance as well.

like image 374
BrunoLM Avatar asked Aug 23 '10 13:08

BrunoLM


People also ask

How do you compare values in two lists in Python?

The cmp() function is a built-in method in Python used to compare the elements of two lists. The function is also used to compare two elements and return a value based on the arguments passed. This value can be 1, 0 or -1.

How do I compare two Excel lists for differences?

On the Home tab, go to Editing group, and click Find & Select > Go To Special… Then select Row differences and click the OK button. The cells whose values are different from the comparison cell in each row are colored.

How do I compare two lists of numbers in Excel?

To use the method, first, select the lists you want to compare in your spreadsheet. While your lists are highlighted, in Excel's ribbon at the top, click the “Home” tab. On the “Home” tab, in the “Styles” section, click Conditional Formatting > Highlight Cells Rules > Duplicate Values.


3 Answers

list1.Intersect(list2).Any()

This will be most performant as it uses HashSets.

like image 125
Tim Rogers Avatar answered Oct 13 '22 16:10

Tim Rogers


There are a few different ways to do it:

Intersect

If the result of the intersection result in 1 or more elements, it means that at least one equal element.

var result = list01.Intersect(list02);
bool hasElement = result.Any();

I recommend the use of this method.

It is possible to pass a IEqualityComparer<T> as the second parameter in case you need to compare complex types.

Except

If the result of a except has different amount of elements in total, it means that there is at least one equal element.

var result = list01.Except(list02);
bool hasElement = result.Count() != list01.Count;

It is possible to pass a IEqualityComparer<T> as the second parameter in case you need to compare complex types.

Any

If any element in the list01 is equal any element in the list02, it means that there is at least one equal element.

bool hasElement = list01.Any(e => list02.Any(o => o == e));

Any e IndexOf

If any element in the list01 is found in list02, it means that there is at lease one equal element.

bool hasElement = list01.Any(e => list02.IndexOf(e) != -1);

The disadvantage of IndexOf is that you can't pass a IEqualityComparer<T>, instead it will always use the default, EqualityComparer<T>.Default.


Performance

In a big list, list01.Any(e => list02.Any(o => o == e)) will have a good performance only if one of the values from the beginning of the first in contained in the second list. Otherwise the performance will be awful, as the iterations are sequential.

In a performance test I got the following results:

Lists with 5 elements each, tested 10000000 times.

Intersect     : 00:00:02.9260135
Except        : 00:00:03.4404527
AnyAny        : 00:00:06.5709693
AnyIndexOf    : 00:00:01.9882278

Lists with 100000 elements each, tested 500 times. The last element of list02 is equal to the third element in list01:

Intersect     : 00:00:02.4397784
Except        : 00:00:04.2595364
AnyAny        : 00:00:02.9761128
AnyIndexOf    : 00:00:00.0919344

Lists with 100000 elements each, tested 500 times. The last element of list02 is equal to the last element in list01.

Intersect     : 00:00:02.4927969
Except        : 00:00:04.2668677
AnyAny        : more than a minute and I dropped the test
AnyIndexOf    : more than a minute and I dropped the test
like image 9
BrunoLM Avatar answered Oct 13 '22 17:10

BrunoLM


Enumerable.Except & Enumerable.Intersect.

like image 5
leppie Avatar answered Oct 13 '22 17:10

leppie