On Tue, 22 May 2018 10:59:24 -0700 Sankar <[email protected]> wrote:
>
> Is there a way to use a slice as a key to a map ? If I try, I get a
> compilation error: `invalid map key type`.
>
> I tried creating a custom type, which contains an array as the element.
> However, I could not find out what method to implement for my custom
> type(such as: func (i *myType) Equal(j *myType) bool ) that would make the
> equality operator call this function.
>
> An example program, where I needed to use a slice/array as the key of a map
> is also available at: https://play.golang.org/p/kXwAaHckL76
>
> Is there a way (or alternate solutions) to use a slice as the map key ?
Your example problem is solvable without needing a map[slice].
Outline: sort the array, put duplicates in the same slot. Thus
for example [-1, 0, 1, 2, -1, -4] turns into
[{-4},{-1,-1},{0},{1},{2}]
Now for a given number N with D duplicates you can pick 0..D
elements. Thus for the example list, we have 2x3x2x2x2
combinations with duplicates (i.e. 48 instead of 64, if there
were no duplicates) containing 0 to 5 numbers. Throw out all
non-triples as well as nonzero triples.
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.