I have two List<T> objects (where T is the same type for both objects), and I need to be able to determine whether they contain the same set of values, even if the values aren't in the same order.
Do the objects have any built-in mechanisms to accomplish this, or do I need to write my own algorithm?
Or perhaps, should I be using a different type of collection, rather than List<T>?
If I were to write my own algorithm, it would probably consist of the following steps - I'll try to optimize this in the final version, if I go this route:
- Do the two collections contain the same number of values? If not return false.
- Count the number of times each value appears in each collection, return false if the counts aren't equal.
- If I reach the end of both collections without any inequality in value counts, return true.
I know there are some caveats to this, such as the fact that T has to be comparable - I'm using the default comparison for now (e.g. .Equals() ) with appropriate constraints defined for the generic type.