Hash function on list independant of order of items in it

后端 未结 9 829
无人共我
无人共我 2021-01-19 21:54

I want to have a dictionary that assigns a value to a set of integers.

For example key is [1 2 3] and value will have certain

相关标签:
9条回答
  • 2021-01-19 22:53

    Use a HashSet<T> and HashSet<T>.CreateSetComparer(), which returns an IEqualityComparer<HashSet<T>>.

    0 讨论(0)
  • 2021-01-19 22:58

    Create your own type that implements IEnumerable<T>.

    Override GetHashCode. In it, sort your collection, call and return ToArray().GetHashCode().

    0 讨论(0)
  • 2021-01-19 23:00

    If the range of the values in key happens to be limited to low-ish positive integers, you could map each one to a prime number using a simple lookup, then multiply them together to arrive at the value.

    Using the example in the question:

    [1, 2, 3] maps to 2 x 3 x 5 = 30
    [3, 2, 1] maps to 5 x 3 x 2 = 30
    
    0 讨论(0)
提交回复
热议问题