* Mikael Gustavsson <slv...@gmail.com> [161208 03:55]:
> There's no need to use maps, you want to follow this pattern:
> 
> for i := range items {
>   if i==0 || !grouped(i-1, i) {
>     appendToResult
>   }
>   appendToGroup
> }
> 
> Here's the full example: https://play.golang.org/p/1e0rDDmq7b

Note that this only works if the input slice is sorted, which it is in
the example data given by the OP, but that constraint was not explicitly
stated.  If the input is not sorted, you must either sort it first, use
a map, or perform a search in each iteration.

If the input data is not guaranteed to be sorted, minimal changes to the
above would work: https://play.golang.org/p/4GsVUV_fBs

The map is only used to perform the search efficiently, and is no longer
used after the for loop.

...Marvin

-- 
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 golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to