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.
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.
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.
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.
list1.Intersect(list2).Any()
This will be most performant as it uses HashSets.
There are a few different ways to do it:
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.
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.
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));
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
.
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
Enumerable.Except
& Enumerable.Intersect
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With