//Here I have a List of Lists
List<List<T>> SelectionList = new List<List<T>>();
//My current code to compare lists
for(int i = 0; i < SelectionList.Count; i )
{
for(int j = 0; j < SelectionList.Count; j )
{
if (SelectionList[i].Equals(SelectionList[j]))
{
SelectionList.Remove(SelectionList[j]);
}
}
}
//Note: The above code supposedly works, in cases where the contents of both the lists are ordered ie., they are in same index, but in my lists they might be same but list contents are shuffled. in this case it fails to identify.
//Basically I need to remove any repetitions of same list in my list of lists;
CodePudding user response:
If, for each list, each possible value appears at most once in the list, you could use a Dictionary<T,int>
to store how often an element appears. Then you perform the following steps:
- Iterate over the lists, and, for each list, do the following: For each list element
k
, check if the dictionary contains it as a key. If it does not, then add keyk
with value1
to your dictionary. If it does, then increment the value for keyk
by1
. - Iterate over the dictionary's elements and check that all values are
2
(if you have two lists) orn
(if you haven
lists).
CodePudding user response:
Your way is not correct, as you said, try this:
you should iterate the following procedure over your list-Of-Lists;
private bool Compare(List<T> List1,List<T> List2)
{
var infirstNotSecond = list1.Except(list2).ToList();
var insecondNotFirst = list2.Except(list1).ToList();
return !infirstNotSecond.Any() && !insecondNotFirst.Any();
}