In addition to other answers, you can speed up the process by creating a low-cost hash just built from XOR among all the elements of each List. You will not need to order your list, and all you get is an int , which is easier and faster to store than strings.
Then you just need to use the received XORed number as the key to the Hashtable and check for the presence of the key before inserting it. If an existing key already exists, only then will you sort the relevant lists and compare them.
You still need to compare them if you find a match, because there may be some collisions using plain XOR.
I think the result will be much faster and will have much less memory than reordering arrays and converting them to strings.
If you have your own implementation of List<> , you can create an XOR key generation inside it so that it is recalculated during each operation in the list.
This will speed up checking for duplicate lists.
code
The following is the first attempt to implement this.
Dictionary<int, List<List<int>>> checkHash = new Dictionary<int, List<List<int>>>(); public bool CheckDuplicate(List<int> theList) { bool isIdentical = false; int xorkey = 0; foreach (int v in theList) xorkey ^= v; List<List<int>> existingLists; checkHash.TryGetValue(xorkey, out existingLists); if (existingLists != null) { // Already in the dictionary. Check each stored list foreach (List<int> li in existingLists) { isIdentical = (theList.Count == li.Count); if (isIdentical) { // Check all elements foreach (int v in theList) { if (!li.Contains(v)) { isIdentical = false; break; } } } if (isIdentical) break; } } if (existingLists == null || !isIdentical) { // never seen this before, add it List<List<int>> newList = new List<List<int>>(); newList.Add(theList); checkHash.Add(xorkey, newList); } return isIdentical; }
Not the most elegant or easy to read at a glance, it's more like hacks, and I'm not even sure that it works better than the more elegant version from Guffa.
What he does, though he takes care of the collision in the XOR key, storing the List<int> lists in the dictionary.
If a duplicate key is found, we look at each previously saved list until we find a mismatch.
A good point for the code is that it should probably be as fast as you could get in most cases and still faster than compiling lines in a collision.