Given a set of integers, a "functional group", is there a better way to GetHashCode of the integers where the positions of the numbers doesn't affect the hash?
void Main()
{
int[] ints = { 10001, 10002, 10003, 10004, 10005 };
int hash = GetHashCode(ints);
Console.WriteLine("hash={0}", hash);
}
int GetHashCode(IEnumerable<int> integers)
{
IEnumerator<int> intEnum = integers.GetEnumerator();
if(intEnum.MoveNext()==false) return 0;
int hash = 0;
unchecked {
hash = intEnum.Current.GetHashCode();
for(;intEnum.MoveNext()==true;)
hash = 31 * hash + intEnum.Current.GetHashCode();
}
return hash;
}
Output of this is: hash=954101523 If I swap 10003 and 10002 i get: hash=954130353
Besides sorting the list before getting the hash, is there a better alternative that wont change if the items in the list positions change?
The list of integers basically represents a set of record Ids that are a "functional group", so the "functional group" is really the key, and not really dependent on the order