69
I would like to write a function GetHashCodeOfList() which returns a hash-code of a list of strings regardless of order. Given 2 lists with the same strings should return the same hash-code.
ArrayList list1 = new ArrayList()
list1.Add("String1");
list1.Add("String2");
list1.Add("String3");
ArrayList list2 = new ArrayList()
list2.Add("String3");
list2.Add("String2");
list2.Add("String1");
GetHashCodeOfList(list1) = GetHashCodeOfList(list2) //this should be equal.
I had a few thoughts:
I can first sort the list, then combine the sorted list into 1 long string and then call
GetHashCode(). However sorting is a slow operation.I can get the hash of each individual string (by calling
string.GetHashCode()) in the list, then multiplying all hashes and calling ModUInt32.MaxValue. For example:"String1".GetHashCode() * "String2".GetHashCode * … MOD UInt32.MaxValue. But this results in a number overflow.
Does anyone have any thoughts?
Thanks in advance for your help.