1. Two lists if there are duplicate elements (such as List1:a,b,a list2:b,b,a), it is not possible to determine equality by including a relationship.
There are two options, one of which is to sort the two lists and then compare them sequentially. The other option is to calculate the duplicates of each element before comparing them.
The first option is too obvious and time is too big
The second is space-time, and only needs to be traversed without ordering.
/// <summary> ///Determines whether two sets are equal (all elements and numbers are equal)/// </summary> /// <typeparam name= "T" >collection Element type</typeparam> /// <param name= "Sourcecollection" >List of source collections</param> /// <param name= "Targetcollection" >Target Collection List</param> /// <returns>two sets equal returns True, otherwise false</returns> Public Static BOOLEquallist<t> ( ThisIlist<t> sourcecollection, ilist<t> targetcollection)whereT:iequatable<t> { //An empty collection returns false directly, even if two are empty collections, and also returns false if(Sourcecollection = =NULL|| Targetcollection = =NULL) { return false; } if(Object. ReferenceEquals (Sourcecollection, targetcollection)) {return true; } if(Sourcecollection.count! =targetcollection.count) {return false; } varSourcecollectionstaticsdict =sourcecollection.statisticrepetition (); varTargetcollectionstaticsdict =targetcollection.statisticrepetition (); returnsourcecollectionstaticsdict.equaldictionary (targetcollectionstaticsdict); } /// <summary> ///determine if two dictionaries are equal (all dictionary entries correspond to equal values)/// </summary> /// <typeparam name= "TKey" >Dictionary Item Type</typeparam> /// <typeparam name= "TValue" >Dictionary value Types</typeparam> /// <param name= "Sourcedictionary" >Source Dictionary</param> /// <param name= "Targetdictionary" >Target Dictionary</param> /// <returns>Two dictionaries are equal to return true, otherwise false</returns> Public Static BOOLEqualdictionary<tkey, Tvalue> ( ThisDictionary<tkey, tvalue> sourcedictionary, Dictionary<tkey, tvalue>targetdictionary)whereTkey:iequatable<tkey>whereTvalue:iequatable<tvalue> { //NULL dictionary returns false directly, even if two are empty dictionaries, also return false if(Sourcedictionary = =NULL|| Targetdictionary = =NULL) { return false; } if(Object. ReferenceEquals (Sourcedictionary, targetdictionary)) {return true; } if(Sourcedictionary.count! =targetdictionary.count) {return false; } //compare keys and value for two dictionaries foreach(varIteminchsourcedictionary) { //Not equal if the target dictionary does not contain any of the source dictionaries if(!Targetdictionary.containskey (item. Key)) {return false; } //Not equal if the value of the same dictionary item is not equal if(!Targetdictionary[item. Key]. Equals (item. Value)) {return false; } } return true; } /// <summary> ///statistics collection of duplicates, and returns a dictionary/// </summary> /// <typeparam name= "T" >collection Element type</typeparam> /// <param name= "Sourcecollection" >Statistics Collection List</param> /// <returns>returns a collection element and a duplicate number of dictionaries</returns> Private StaticDictionary<t,int> statisticrepetition<t> ( ThisIenumerable<t> sourcecollection)whereT:iequatable<t> { varCollectionstaticsdict =NewDictionary<t,int>(); foreach(varIteminchsourcecollection) { if(Collectionstaticsdict.containskey (item)) {Collectionstaticsdict[item]++; } Else{collectionstaticsdict.add (item,1); } } returncollectionstaticsdict; }
2
Public classCommontest {/// <summary> ///Collection Equality Comparison/// </summary>[Fact] Public voidlistequal_tests () {varSourceList =Newlist<string>() { "a", "b", "a" }; varTargetList =Newlist<string>() { "b", "b", "a" }; varRESP =sourcelist.equallist (TargetList); Assert.false (RESP); } /// <summary> ///Collection Equality Comparison/// </summary>[Fact] Public voidlistequal2_tests () {varSourceList =Newlist<string>() { "a", "b", }; varTargetList =Newlist<string>() { "b", "a" }; varRESP =sourcelist.equallist (TargetList); Assert.true (RESP); } }
C # Determines whether two sets (List) are equal