Skip to main content
edited body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

No matter what fancy syntax features you are using, this is always going to be an operation with an algorithmic complexity of \$O(n * m)\$ (with n and m being the sizes of the respective lists). withWith two unordered lists, there is no way around iterating every single element of the first list and comparing it with every single element of the second. So no matter if you use a lambdaRemoveAll, a LINQ function or write the algorithm on your own loops, you will achieve micro-optimizations at best.

However, if you change the type of the _list from a List to a SortedSet or HashSet, then you can do this:

_set.ExceptWith(_listToRemove);

The ExceptWith(IEnumerable<T> other) method of both SortedSet and HashSet "is an \$O(n)\$ operation, where n is the number of elements in the other parameter.".

If you are not sure which flavor of ISet is more appropriate for your use-case, check this question on Stackoverflow (there is also a lot of interesting debate in the comments on the answers).

No matter what fancy syntax features you are using, this is always going to be an operation with an algorithmic complexity of \$O(n * m)\$ (with n and m being the sizes of the respective lists). with two unordered lists, there is no way around iterating every single element of the first list and comparing it with every single element of the second. So no matter if you use a lambda, a LINQ function or your own loops, you will achieve micro-optimizations at best.

However, if you change the type of the _list from a List to a SortedSet or HashSet, then you can do this:

_set.ExceptWith(_listToRemove);

The ExceptWith(IEnumerable<T> other) method of both SortedSet and HashSet "is an \$O(n)\$ operation, where n is the number of elements in the other parameter.".

If you are not sure which flavor of ISet is more appropriate for your use-case, check this question on Stackoverflow (there is also a lot of interesting debate in the comments on the answers).

No matter what fancy syntax features you are using, this is always going to be an operation with an algorithmic complexity of \$O(n * m)\$ (with n and m being the sizes of the respective lists). With two unordered lists, there is no way around iterating every single element of the first list and comparing it with every single element of the second. So no matter if you use RemoveAll, a LINQ function or write the algorithm on your own, you will achieve micro-optimizations at best.

However, if you change the type of the _list from a List to a SortedSet or HashSet, then you can do this:

_set.ExceptWith(_listToRemove);

The ExceptWith(IEnumerable<T> other) method of both SortedSet and HashSet "is an \$O(n)\$ operation, where n is the number of elements in the other parameter.".

If you are not sure which flavor of ISet is more appropriate for your use-case, check this question on Stackoverflow (there is also a lot of interesting debate in the comments on the answers).

added 305 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

No matter what fancy syntax features you are using, this is always going to be an operation with an algorithmic complexity of \$O(n * m)\$ (with n and m being the sizes of the respective lists). with two unordered lists, there is no way around iterating every single element of the first list and comparing it with every single element of the second. So no matter if you use a lambda, a LINQ function or your own loops, you will achieve micro-optimizations at best.

However, if you change the type of the _list from a List to a SortedSet or HashSet, then you can do this:

_set.ExceptWith(_listToRemove);

The ExceptWith(IEnumerable<T> other) method of both SortedSet and HashSet "is an \$O(n)\$ operation, where n is the number of elements in the other parameter.".

If you are not sure if a Hashset or a SortedSetwhich flavor of ISet is more appropriate for your use-case, check this question on Stackoverflow (there is also a lot of interesting debate in the comments on the answers).

No matter what fancy syntax features you are using, this is always going to be an operation with an algorithmic complexity of \$O(n * m)\$ (with n and m being the sizes of the respective lists). with two unordered lists, there is no way around iterating every single element of the first list and comparing it with every single element of the second. So no matter if you use a lambda, a LINQ function or your own loops, you will achieve micro-optimizations at best.

However, if you change the type of the _list from a List to a SortedSet or HashSet, then you can do this:

_set.ExceptWith(_listToRemove);

The ExceptWith(IEnumerable<T> other) method of both SortedSet and HashSet "is an \$O(n)\$ operation, where n is the number of elements in the other parameter.".

If you are not sure if a Hashset or a SortedSet is more appropriate for your use-case, check this question on Stackoverflow (there is also a lot of interesting debate in the comments on the answers).

No matter what fancy syntax features you are using, this is always going to be an operation with an algorithmic complexity of \$O(n * m)\$ (with n and m being the sizes of the respective lists). with two unordered lists, there is no way around iterating every single element of the first list and comparing it with every single element of the second. So no matter if you use a lambda, a LINQ function or your own loops, you will achieve micro-optimizations at best.

However, if you change the type of the _list from a List to a SortedSet or HashSet, then you can do this:

_set.ExceptWith(_listToRemove);

The ExceptWith(IEnumerable<T> other) method of both SortedSet and HashSet "is an \$O(n)\$ operation, where n is the number of elements in the other parameter.".

If you are not sure which flavor of ISet is more appropriate for your use-case, check this question on Stackoverflow (there is also a lot of interesting debate in the comments on the answers).

added 305 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

No matter what fancy syntax features you are using, this is always going to be an operation with an algorithmic complexity of \$O(n * m)\$ (with n and m being the sizes of the respective lists). So no matter if you use a lambda, a LINQ query or your own loops, you will achieve micro-optimizations at best. Because with two unordered lists, there is no way around iterating every single element of the first list and comparing it with every single element of the second. So no matter if you use a lambda, a LINQ function or your own loops, you will achieve micro-optimizations at best.

However, if you change the type of the _list from a List to a SortedSet or HashSet, then you can do this:

_set.ExceptWith(_listToRemove);

SortedSet.ExceptWith(IEnumerable<T> other) The ExceptWith(IEnumerable<T> other) method of both SortedSet and HashSet "is an \$O(n)\$ operation, where n is the number of elements in the other parameter.".

If you are not sure if a Hashset or a SortedSet is more appropriate for your use-case, check this question on Stackoverflow (there is also a lot of interesting debate in the comments on the answers).

No matter what fancy syntax features you are using, this is always going to be an operation with an algorithmic complexity of \$O(n * m)\$ (with n and m being the sizes of the respective lists). So no matter if you use a lambda, a LINQ query or your own loops, you will achieve micro-optimizations at best. Because with two unordered lists, there is no way around iterating every single element of the first list and comparing it with every single element of the second.

However, if you change the type of the _list from a List to a SortedSet, then you can do this:

_set.ExceptWith(_listToRemove);

SortedSet.ExceptWith(IEnumerable<T> other) "is an \$O(n)\$ operation, where n is the number of elements in the other parameter."

No matter what fancy syntax features you are using, this is always going to be an operation with an algorithmic complexity of \$O(n * m)\$ (with n and m being the sizes of the respective lists). with two unordered lists, there is no way around iterating every single element of the first list and comparing it with every single element of the second. So no matter if you use a lambda, a LINQ function or your own loops, you will achieve micro-optimizations at best.

However, if you change the type of the _list from a List to a SortedSet or HashSet, then you can do this:

_set.ExceptWith(_listToRemove);

The ExceptWith(IEnumerable<T> other) method of both SortedSet and HashSet "is an \$O(n)\$ operation, where n is the number of elements in the other parameter.".

If you are not sure if a Hashset or a SortedSet is more appropriate for your use-case, check this question on Stackoverflow (there is also a lot of interesting debate in the comments on the answers).

Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345
Loading