FAQ
Hello,

I am looking for an easy way to merge or "concatenate" two slices together
(but without duplicates)

For example :
[]string{a,b,c,e} + []string{b,c,d} = []string{a,b,c,e,d}


Thank you in advance.

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

Search Discussions

  • Michael Jones at Sep 4, 2015 at 7:46 pm
    How easy this will be depends on the answer you seek from

    []string{e,c,b,a} + []string{d,c,b} =


    Michael Jones, CEO • michael@wearality.com • +1 650 656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022
    On Sep 4, 2015, at 12:28 PM, Stéphane phenetas wrote:

    Hello,

    I am looking for an easy way to merge or "concatenate" two slices together (but without duplicates)

    For example :
    []string{a,b,c,e} + []string{b,c,d} = []string{a,b,c,e,d}


    Thank you in advance.


    --
    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+unsubscribe@googlegroups.com For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dustin at Sep 4, 2015 at 7:46 pm
    Do you care about order? You could just load them both into a map and build
    a new slice. Or sort both slices and do an iterative merge (sans
    duplicates).
    On Friday, September 4, 2015 at 12:28:39 PM UTC-7, Stéphane phenetas wrote:

    Hello,

    I am looking for an easy way to merge or "concatenate" two slices together
    (but without duplicates)

    For example :
    []string{a,b,c,e} + []string{b,c,d} = []string{a,b,c,e,d}


    Thank you in advance.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Stéphane phenetas at Sep 4, 2015 at 7:55 pm
    I don't care at all about order, my only concern is not to lose any
    elements of course.

    For your example Michael, I am expecting this answer :

    []string{e,c,b,a} + []string{d,c,b} = []string{e,c,b,a,d} *OR*
    []string{e,c,b,d,a} *OR* *any other possibility as long as they are **a b c
    d e**, and only one time each in the solution.*

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Rob Pike at Sep 4, 2015 at 7:56 pm
    Then the solution that stages through a map is the simplest to code,
    provided the slice elements can be used as map keys.

    -rob

    On Fri, Sep 4, 2015 at 12:55 PM, Stéphane phenetas wrote:

    I don't care at all about order, my only concern is not to lose any
    elements of course.

    For your example Michael, I am expecting this answer :

    []string{e,c,b,a} + []string{d,c,b} = []string{e,c,b,a,d} *OR*
    []string{e,c,b,d,a} *OR* *any other possibility as long as they are **a b
    c d e**, and only one time each in the solution.*

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Kiki Sugiaman at Sep 4, 2015 at 9:31 pm
    Getting general: If we want to use the map as a set (only care about the
    keys), what should be the type of the values? struct{}?

    On 05/09/15 05:56, Rob Pike wrote:
    Then the solution that stages through a map is the simplest to code,
    provided the slice elements can be used as map keys.

    -rob


    On Fri, Sep 4, 2015 at 12:55 PM, Stéphane phenetas wrote:

    I don't care at all about order, my only concern is not to lose
    any elements of course.

    For your example Michael, I am expecting this answer :

    []string{e,c,b,a} + []string{d,c,b} = []string{e,c,b,a,d} *OR*
    []string{e,c,b,d,a} *OR* _any other possibility as long as they
    are _/a b c d e/_, and only one time each in the solution._
    --
    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+unsubscribe@googlegroups.com
    For more options, visit https://groups.google.com/d/optout.


    --
    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+unsubscribe@googlegroups.com
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Matt Harden at Sep 5, 2015 at 2:39 am

    On Fri, Sep 4, 2015 at 4:31 PM Kiki Sugiaman wrote:

    Getting general: If we want to use the map as a set (only care about the
    keys), what should be the type of the values? struct{}?
    Yes, struct{} is the best answer. It has a size of zero so it's space
    efficient. Bool would be confusing, because if the value were false, would
    that key be considered part of the set or not?

    On 05/09/15 05:56, Rob Pike wrote:
    Then the solution that stages through a map is the simplest to code,
    provided the slice elements can be used as map keys.

    -rob


    On Fri, Sep 4, 2015 at 12:55 PM, Stéphane phenetas <phenetas@gmail.com
    wrote:

    I don't care at all about order, my only concern is not to lose
    any elements of course.

    For your example Michael, I am expecting this answer :

    []string{e,c,b,a} + []string{d,c,b} = []string{e,c,b,a,d} *OR*
    []string{e,c,b,d,a} *OR* _any other possibility as long as they
    are _/a b c d e/_, and only one time each in the solution._
    --
    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+unsubscribe@googlegroups.com
    For more options, visit https://groups.google.com/d/optout.

    --
    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+unsubscribe@googlegroups.com
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Val at Sep 5, 2015 at 11:21 am
    "without duplicates" implies "comparable with ==", and
    "comparable with ==" IFF "can be used as map keys".

    Unless I'm missing a corner case.

    On Friday, September 4, 2015 at 9:56:47 PM UTC+2, Rob 'Commander' Pike
    wrote:
    Then the solution that stages through a map is the simplest to code,
    provided the slice elements can be used as map keys.

    -rob


    On Fri, Sep 4, 2015 at 12:55 PM, Stéphane phenetas <phen...@gmail.com
    <javascript:>> wrote:
    I don't care at all about order, my only concern is not to lose any
    elements of course.

    For your example Michael, I am expecting this answer :

    []string{e,c,b,a} + []string{d,c,b} = []string{e,c,b,a,d} *OR*
    []string{e,c,b,d,a} *OR* *any other possibility as long as they are **a
    b c d e**, and only one time each in the solution.*

    --
    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...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dan Kortschak at Sep 5, 2015 at 11:42 am

    On Sat, 2015-09-05 at 04:21 -0700, Val wrote:
    "without duplicates" implies "comparable with ==", and
    "comparable with ==" IFF "can be used as map keys".

    Unless I'm missing a corner case.
    Is [][]int{{1}, {1}, {2}} a value with duplicates? []int is not a
    map-keyable type.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Val at Sep 5, 2015 at 11:50 am
    Thanks Kortschak, that's right the flaw was in my first statement.
    When type is not comparable with ==, the requirement "without duplicates"
    may still hold for some other equivalence relations e.g. reflect.DeepEquals
    <http://golang.org/pkg/reflect/#DeepEqual> .
    And indeed a map won't work out of the box, we will need another strategy.
    On Saturday, September 5, 2015 at 1:42:33 PM UTC+2, kortschak wrote:
    On Sat, 2015-09-05 at 04:21 -0700, Val wrote:
    "without duplicates" implies "comparable with ==", and
    "comparable with ==" IFF "can be used as map keys".

    Unless I'm missing a corner case.
    Is [][]int{{1}, {1}, {2}} a value with duplicates? []int is not a
    map-keyable type.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Stéphane phenetas at Sep 4, 2015 at 7:57 pm
    So in the above example []string{e,c,b,a} + []string{d,c,b} =
    []string{e,c,b,a,d}

    Because they are 5 unique letters, they are 5*4*3*2*1 possible solutions
    that could answer my question :)

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Alexm at Sep 4, 2015 at 7:56 pm
    Here is an example of merging two slices using a map:
    http://play.golang.org/p/qnQlXs5tnT.

    Note as Dustin mentioned the results are out of order due to maps having no
    specified order when ranging over them.

    Another improvement that could be made is to make the merge function a
    variadic function and you will be able to merge an arbitrary number of
    slices.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dan Kortschak at Sep 4, 2015 at 8:44 pm
    There's this https://github.com/xtgo/set

    On 05/09/2015, at 4:58 AM, "Stéphane phenetas" wrote:

    Hello,

    I am looking for an easy way to merge or "concatenate" two slices together (but without duplicates)

    For example :
    []string{a,b,c,e} + []string{b,c,d} = []string{a,b,c,e,d}


    Thank you in advance.


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

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Michael Jones at Sep 4, 2015 at 9:10 pm
    Since order does not matter then the easy way is the one alexm posted. Here are some subtle changes to it:

    http://play.golang.org/p/g3QTWcy9D- <http://play.golang.org/p/g3QTWcy9D->

    Another, slower way but that has no time or space overhead is to add elements from one slice to the other, one at a time, after verifying that the are not already in that first slice. This is probably better for tiny slices but would be slower for large ones. (If there is a chance that the slices are individually not unique, then you do it in two steps adding each original to an empty slice.)

    The linked way is likely better.

    Michael


    Michael Jones, CEO • michael@wearality.com • +1 650 656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022
    On Sep 4, 2015, at 1:44 PM, Dan Kortschak wrote:

    There's this https://github.com/xtgo/set <https://github.com/xtgo/set>
    On 05/09/2015, at 4:58 AM, "Stéphane phenetas" wrote:

    Hello,

    I am looking for an easy way to merge or "concatenate" two slices together (but without duplicates)

    For example :
    []string{a,b,c,e} + []string{b,c,d} = []string{a,b,c,e,d}


    Thank you in advance.


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

    --
    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+unsubscribe@googlegroups.com For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dustin at Sep 4, 2015 at 9:17 pm
    I actually like allocating the slice with a cap instead of a len that fits
    so you can use append instead of an index.

    http://play.golang.org/p/htFxY5TNyH
    On Friday, September 4, 2015 at 2:11:09 PM UTC-7, Michael Jones wrote:

    Since order does not matter then the easy way is the one alexm posted.
    Here are some subtle changes to it:

    http://play.golang.org/p/g3QTWcy9D-

    Another, slower way but that has no time or space overhead is to add
    elements from one slice to the other, one at a time, after verifying that
    the are not already in that first slice. This is probably better for tiny
    slices but would be slower for large ones. (If there is a chance that the
    slices are individually not unique, then you do it in two steps adding each
    original to an empty slice.)

    The linked way is likely better.

    Michael


    Michael Jones, CEO • mic...@wearality.com <javascript:> • +1 650
    656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022

    On Sep 4, 2015, at 1:44 PM, Dan Kortschak <dan.ko...@adelaide.edu.au
    <javascript:>> wrote:

    There's this https://github.com/xtgo/set

    On 05/09/2015, at 4:58 AM, "Stéphane phenetas" <phen...@gmail.com
    <javascript:>> wrote:

    Hello,

    I am looking for an easy way to merge or "concatenate" two slices together
    (but without duplicates)

    For example :
    []string{a,b,c,e} + []string{b,c,d} = []string{a,b,c,e,d}


    Thank you in advance.


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


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

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Val at Sep 5, 2015 at 11:28 am
    When I measured allocating (A) with len + direct indexing, and (B) with cap
    + appending,
    I found that (A) was faster by a non-negligible factor.

    But I totally agree that random microbenchmarking matters far less than
    readability and style consistency. Also, (A) works only if the final length
    is known at allocation point.
    On Friday, September 4, 2015 at 11:17:24 PM UTC+2, Dustin wrote:

    I actually like allocating the slice with a cap instead of a len that fits
    so you can use append instead of an index.

    http://play.golang.org/p/htFxY5TNyH
    On Friday, September 4, 2015 at 2:11:09 PM UTC-7, Michael Jones wrote:

    Since order does not matter then the easy way is the one alexm posted.
    Here are some subtle changes to it:

    http://play.golang.org/p/g3QTWcy9D-

    Another, slower way but that has no time or space overhead is to add
    elements from one slice to the other, one at a time, after verifying that
    the are not already in that first slice. This is probably better for tiny
    slices but would be slower for large ones. (If there is a chance that the
    slices are individually not unique, then you do it in two steps adding each
    original to an empty slice.)

    The linked way is likely better.

    Michael


    Michael Jones, CEO • mic...@wearality.com • +1 650 656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022

    On Sep 4, 2015, at 1:44 PM, Dan Kortschak <dan.ko...@adelaide.edu.au>
    wrote:

    There's this https://github.com/xtgo/set

    On 05/09/2015, at 4:58 AM, "Stéphane phenetas" wrote:

    Hello,

    I am looking for an easy way to merge or "concatenate" two slices
    together (but without duplicates)

    For example :
    []string{a,b,c,e} + []string{b,c,d} = []string{a,b,c,e,d}


    Thank you in advance.


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


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

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Michael Jones at Sep 5, 2015 at 2:34 pm
    Certainly non-negligible. Save this to your machine as append_test.go and then "go test -bench-.”

    http://play.golang.org/p/rjNV7yHoS0 <http://play.golang.org/p/rjNV7yHoS0>

    You will see something like:

    mbp:append mtj$ go test -v -bench=.
    testing: warning: no tests to run
    PASS
    BenchmarkAppendA10-8 5000000 287 ns/op
    BenchmarkAppendA100-8 2000000 790 ns/op
    BenchmarkAppendA1000-8 300000 4039 ns/op
    BenchmarkAppendA10000-8 30000 59177 ns/op
    BenchmarkAppendA100000-8 2000 750870 ns/op
    BenchmarkAppendB10-8 20000000 61.0 ns/op
    BenchmarkAppendB100-8 5000000 245 ns/op
    BenchmarkAppendB1000-8 1000000 1849 ns/op
    BenchmarkAppendB10000-8 100000 19797 ns/op
    BenchmarkAppendB100000-8 10000 204953 ns/op
    BenchmarkAppendC10-8 30000000 55.0 ns/op
    BenchmarkAppendC100-8 10000000 192 ns/op
    BenchmarkAppendC1000-8 1000000 1444 ns/op
    BenchmarkAppendC10000-8 100000 12773 ns/op
    BenchmarkAppendC100000-8 10000 127774 ns/op

    The war of performance is won one battle at a time.


    Michael Jones, CEO • michael@wearality.com • +1 650 656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022
    On Sep 5, 2015, at 4:28 AM, Val wrote:

    When I measured allocating (A) with len + direct indexing, and (B) with cap + appending,
    I found that (A) was faster by a non-negligible factor.

    But I totally agree that random microbenchmarking matters far less than readability and style consistency. Also, (A) works only if the final length is known at allocation point.

    On Friday, September 4, 2015 at 11:17:24 PM UTC+2, Dustin wrote:
    I actually like allocating the slice with a cap instead of a len that fits so you can use append instead of an index.

    http://play.golang.org/p/htFxY5TNyH <http://play.golang.org/p/htFxY5TNyH>

    On Friday, September 4, 2015 at 2:11:09 PM UTC-7, Michael Jones wrote:
    Since order does not matter then the easy way is the one alexm posted. Here are some subtle changes to it:

    http://play.golang.org/p/g3QTWcy9D- <http://play.golang.org/p/g3QTWcy9D->

    Another, slower way but that has no time or space overhead is to add elements from one slice to the other, one at a time, after verifying that the are not already in that first slice. This is probably better for tiny slices but would be slower for large ones. (If there is a chance that the slices are individually not unique, then you do it in two steps adding each original to an empty slice.)

    The linked way is likely better.

    Michael


    Michael Jones, CEO • mic...@wearality.com <> • +1 650 656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022
    On Sep 4, 2015, at 1:44 PM, Dan Kortschak <dan.ko...@adelaide.edu.au <>> wrote:

    There's this https://github.com/xtgo/set <https://github.com/xtgo/set>
    On 05/09/2015, at 4:58 AM, "Stéphane phenetas" <phen...@gmail.com <>> wrote:

    Hello,

    I am looking for an easy way to merge or "concatenate" two slices together (but without duplicates)

    For example :
    []string{a,b,c,e} + []string{b,c,d} = []string{a,b,c,e,d}


    Thank you in advance.


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

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

    --
    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+unsubscribe@googlegroups.com For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dustin at Sep 5, 2015 at 6:48 pm
    I agree indexing should be more efficient in general, I mainly like append
    for readability.

    Interestingly enough, on my computer append beat indexing until around
    10,000 elements were being added. So your mileage may vary.

    λ go test -bench=.
    testing: warning: no tests to run
    PASS
    BenchmarkAppendA10-8 5000000 362 ns/op
    BenchmarkAppendA100-8 1000000 2285 ns/op
    BenchmarkAppendA1000-8 200000 13575 ns/op
    BenchmarkAppendA10000-8 5000 321666 ns/op
    BenchmarkAppendA100000-8 500 4334534 ns/op
    BenchmarkAppendB10-8 20000000 105 ns/op
    BenchmarkAppendB100-8 2000000 682 ns/op
    BenchmarkAppendB1000-8 300000 5506 ns/op
    BenchmarkAppendB10000-8 20000 64023 ns/op
    BenchmarkAppendB100000-8 3000 602049 ns/op
    BenchmarkAppendC10-8 20000000 107 ns/op
    BenchmarkAppendC100-8 2000000 762 ns/op
    BenchmarkAppendC1000-8 200000 7670 ns/op
    BenchmarkAppendC10000-8 20000 60029 ns/op
    BenchmarkAppendC100000-8 3000 663352 ns/op
    On Saturday, September 5, 2015 at 7:34:31 AM UTC-7, Michael Jones wrote:

    Certainly non-negligible. Save this to your machine as append_test.go and
    then "go test -bench-.”

    http://play.golang.org/p/rjNV7yHoS0

    You will see something like:

    mbp:append mtj$ go test -v -bench=.
    testing: warning: no tests to run
    PASS
    BenchmarkAppendA10-8 5000000 287 ns/op
    BenchmarkAppendA100-8 2000000 790 ns/op
    BenchmarkAppendA1000-8 300000 4039 ns/op
    BenchmarkAppendA10000-8 30000 59177 ns/op
    BenchmarkAppendA100000-8 2000 750870 ns/op
    BenchmarkAppendB10-8 20000000 61.0 ns/op
    BenchmarkAppendB100-8 5000000 245 ns/op
    BenchmarkAppendB1000-8 1000000 1849 ns/op
    BenchmarkAppendB10000-8 100000 19797 ns/op
    BenchmarkAppendB100000-8 10000 204953 ns/op
    BenchmarkAppendC10-8 30000000 55.0 ns/op
    BenchmarkAppendC100-8 10000000 192 ns/op
    BenchmarkAppendC1000-8 1000000 1444 ns/op
    BenchmarkAppendC10000-8 100000 12773 ns/op
    BenchmarkAppendC100000-8 10000 127774 ns/op

    The war of performance is won one battle at a time.


    Michael Jones, CEO • mic...@wearality.com <javascript:> • +1 650
    656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022

    On Sep 5, 2015, at 4:28 AM, Val <dele...@gmail.com <javascript:>> wrote:

    When I measured allocating (A) with len + direct indexing, and (B) with
    cap + appending,
    I found that (A) was faster by a non-negligible factor.

    But I totally agree that random microbenchmarking matters far less than
    readability and style consistency. Also, (A) works only if the final length
    is known at allocation point.
    On Friday, September 4, 2015 at 11:17:24 PM UTC+2, Dustin wrote:

    I actually like allocating the slice with a cap instead of a len that
    fits so you can use append instead of an index.

    http://play.golang.org/p/htFxY5TNyH
    On Friday, September 4, 2015 at 2:11:09 PM UTC-7, Michael Jones wrote:

    Since order does not matter then the easy way is the one alexm posted.
    Here are some subtle changes to it:

    http://play.golang.org/p/g3QTWcy9D-

    Another, slower way but that has no time or space overhead is to add
    elements from one slice to the other, one at a time, after verifying that
    the are not already in that first slice. This is probably better for tiny
    slices but would be slower for large ones. (If there is a chance that the
    slices are individually not unique, then you do it in two steps adding each
    original to an empty slice.)

    The linked way is likely better.

    Michael


    Michael Jones, CEO • mic...@wearality.com • +1 650 656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA
    94022

    On Sep 4, 2015, at 1:44 PM, Dan Kortschak <dan.ko...@adelaide.edu.au>
    wrote:

    There's this https://github.com/xtgo/set

    On 05/09/2015, at 4:58 AM, "Stéphane phenetas" <phen...@gmail.com>
    wrote:

    Hello,

    I am looking for an easy way to merge or "concatenate" two slices
    together (but without duplicates)

    For example :
    []string{a,b,c,e} + []string{b,c,d} = []string{a,b,c,e,d}


    Thank you in advance.


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


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

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

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Michael Jones at Sep 5, 2015 at 10:58 pm
    For small arrays, the allocation overhead is most of what we are measuring.


    Michael Jones, CEO • michael@wearality.com • +1 650 656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022
    On Sep 5, 2015, at 11:48 AM, Dustin wrote:

    I agree indexing should be more efficient in general, I mainly like append for readability.

    Interestingly enough, on my computer append beat indexing until around 10,000 elements were being added. So your mileage may vary.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Michael Jones at Sep 6, 2015 at 1:11 am
    [Warning: Pedantry ahead]

    This “easy way to merge” question has already been answered to the OP’s satisfaction. But I like to do things several ways and time them just to understand how that plays out. Here is another, simple way to merge:

    http://play.golang.org/p/J4S5KG51GX <http://play.golang.org/p/J4S5KG51GX>

    and a test suite for it:

    http://play.golang.org/p/OFeAHxYLPf <http://play.golang.org/p/OFeAHxYLPf>

    This “Method A” is inferior in a big-O sense (being O(len(a)*len(b)) instead of O(len(a)+len(b)), but it is simple and does not instantiate or resize a hashtable. So, the question is, at what size of arrays is it slower?

    === RUN TestMergeA
    --- PASS: TestMergeA (0.00s)
    === RUN TestMergeB
    --- PASS: TestMergeB (0.00s)
    PASS
    BenchmarkMergeA10-8 1000000000 62.3 ns/op
    BenchmarkMergeA100-8 10000000 4862 ns/op
    BenchmarkMergeA200-8 3000000 16704 ns/op
    BenchmarkMergeA300-8 1000000 35698 ns/op
    BenchmarkMergeA400-8 1000000 61103 ns/op
    BenchmarkMergeA500-8 500000 92235 ns/op
    BenchmarkMergeA1000-8 200000 314616 ns/op
    BenchmarkMergeA10000-8 2000 27669822 ns/op
    BenchmarkMergeB10-8 30000000 1340 ns/op
    BenchmarkMergeB100-8 3000000 14217 ns/op
    BenchmarkMergeB200-8 2000000 29024 ns/op
    BenchmarkMergeB300-8 1000000 47532 ns/op
    BenchmarkMergeB400-8 1000000 57516 ns/op
    BenchmarkMergeB500-8 500000 85069 ns/op
    BenchmarkMergeB1000-8 300000 158194 ns/op
    BenchmarkMergeB10000-8 30000 1480289 ns/op

    As you can see, the simple loop is faster until len(a)+len(b) nears 800. Something to consider when you want to be fast.


    Michael Jones, CEO • michael@wearality.com • +1 650 656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Michael Jones at Sep 6, 2015 at 1:15 am
    just spotted a slight unfairness in method A. does not create a copy so values not quite comparable. sorry…I need to retest.


    Michael Jones, CEO • michael@wearality.com • +1 650 656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022
    On Sep 5, 2015, at 6:11 PM, Michael Jones wrote:

    [Warning: Pedantry ahead]

    This “easy way to merge” question has already been answered to the OP’s satisfaction. But I like to do things several ways and time them just to understand how that plays out. Here is another, simple way to merge:

    http://play.golang.org/p/J4S5KG51GX <http://play.golang.org/p/J4S5KG51GX>

    and a test suite for it:

    http://play.golang.org/p/OFeAHxYLPf <http://play.golang.org/p/OFeAHxYLPf>

    This “Method A” is inferior in a big-O sense (being O(len(a)*len(b)) instead of O(len(a)+len(b)), but it is simple and does not instantiate or resize a hashtable. So, the question is, at what size of arrays is it slower?

    === RUN TestMergeA
    --- PASS: TestMergeA (0.00s)
    === RUN TestMergeB
    --- PASS: TestMergeB (0.00s)
    PASS
    BenchmarkMergeA10-8 1000000000 62.3 ns/op
    BenchmarkMergeA100-8 10000000 4862 ns/op
    BenchmarkMergeA200-8 3000000 16704 ns/op
    BenchmarkMergeA300-8 1000000 35698 ns/op
    BenchmarkMergeA400-8 1000000 61103 ns/op
    BenchmarkMergeA500-8 500000 92235 ns/op
    BenchmarkMergeA1000-8 200000 314616 ns/op
    BenchmarkMergeA10000-8 2000 27669822 ns/op
    BenchmarkMergeB10-8 30000000 1340 ns/op
    BenchmarkMergeB100-8 3000000 14217 ns/op
    BenchmarkMergeB200-8 2000000 29024 ns/op
    BenchmarkMergeB300-8 1000000 47532 ns/op
    BenchmarkMergeB400-8 1000000 57516 ns/op
    BenchmarkMergeB500-8 500000 85069 ns/op
    BenchmarkMergeB1000-8 300000 158194 ns/op
    BenchmarkMergeB10000-8 30000 1480289 ns/op

    As you can see, the simple loop is faster until len(a)+len(b) nears 800. Something to consider when you want to be fast.


    Michael Jones, CEO • michael@wearality.com • +1 650 656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Michael Jones at Sep 6, 2015 at 2:11 am
    Updated with no change of conclusion:
    http://play.golang.org/p/GdCoVPqRmg

    mbp:append mtj$ go test -v -bench=Merge -benchtime=10s -timeout=9999h
    === RUN TestMergeA
    --- PASS: TestMergeA (0.00s)
    === RUN TestMergeB
    --- PASS: TestMergeB (0.00s)
    PASS
    BenchmarkMergeA10-8 100000000 178 ns/op
    BenchmarkMergeA100-8 3000000 4339 ns/op
    BenchmarkMergeA200-8 1000000 13514 ns/op
    BenchmarkMergeA300-8 500000 28151 ns/op
    BenchmarkMergeA400-8 300000 48820 ns/op
    BenchmarkMergeA500-8 200000 72331 ns/op
    BenchmarkMergeA1000-8 50000 269967 ns/op
    BenchmarkMergeA10000-8 500 26127977 ns/op
    BenchmarkMergeB10-8 10000000 1359 ns/op
    BenchmarkMergeB100-8 1000000 14755 ns/op
    BenchmarkMergeB200-8 500000 30172 ns/op
    BenchmarkMergeB300-8 300000 47152 ns/op
    BenchmarkMergeB400-8 200000 58546 ns/op
    BenchmarkMergeB500-8 200000 84528 ns/op
    BenchmarkMergeB1000-8 100000 170863 ns/op
    BenchmarkMergeB10000-8 10000 1534320 ns/op
    ok append 249.911s


    Michael Jones, CEO • michael@wearality.com • +1 650 656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022
    On Sep 5, 2015, at 6:15 PM, Michael Jones wrote:

    just spotted a slight unfairness in method A. does not create a copy so values not quite comparable. sorry…I need to retest.


    Michael Jones, CEO • michael@wearality.com • +1 650 656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022
    On Sep 5, 2015, at 6:11 PM, Michael Jones wrote:

    [Warning: Pedantry ahead]

    This “easy way to merge” question has already been answered to the OP’s satisfaction. But I like to do things several ways and time them just to understand how that plays out. Here is another, simple way to merge:

    http://play.golang.org/p/J4S5KG51GX <http://play.golang.org/p/J4S5KG51GX>

    and a test suite for it:

    http://play.golang.org/p/OFeAHxYLPf <http://play.golang.org/p/OFeAHxYLPf>

    This “Method A” is inferior in a big-O sense (being O(len(a)*len(b)) instead of O(len(a)+len(b)), but it is simple and does not instantiate or resize a hashtable. So, the question is, at what size of arrays is it slower?

    === RUN TestMergeA
    --- PASS: TestMergeA (0.00s)
    === RUN TestMergeB
    --- PASS: TestMergeB (0.00s)
    PASS
    BenchmarkMergeA10-8 1000000000 62.3 ns/op
    BenchmarkMergeA100-8 10000000 4862 ns/op
    BenchmarkMergeA200-8 3000000 16704 ns/op
    BenchmarkMergeA300-8 1000000 35698 ns/op
    BenchmarkMergeA400-8 1000000 61103 ns/op
    BenchmarkMergeA500-8 500000 92235 ns/op
    BenchmarkMergeA1000-8 200000 314616 ns/op
    BenchmarkMergeA10000-8 2000 27669822 ns/op
    BenchmarkMergeB10-8 30000000 1340 ns/op
    BenchmarkMergeB100-8 3000000 14217 ns/op
    BenchmarkMergeB200-8 2000000 29024 ns/op
    BenchmarkMergeB300-8 1000000 47532 ns/op
    BenchmarkMergeB400-8 1000000 57516 ns/op
    BenchmarkMergeB500-8 500000 85069 ns/op
    BenchmarkMergeB1000-8 300000 158194 ns/op
    BenchmarkMergeB10000-8 30000 1480289 ns/op

    As you can see, the simple loop is faster until len(a)+len(b) nears 800. Something to consider when you want to be fast.


    Michael Jones, CEO • michael@wearality.com • +1 650 656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Giulio Iotti at Sep 6, 2015 at 6:47 am

    On Sunday, September 6, 2015 at 5:12:08 AM UTC+3, Michael Jones wrote:
    Updated with no change of conclusion:
    http://play.golang.org/p/GdCoVPqRmg
    Cool, nice benchmark! I couldn't help thinking that your search should
    really be a binary search and, surprise, the standard library has it
    (sort.Search).

    I've expanded your benchmark here:
    https://github.com/dullgiulio/go-tests/tree/master/append

    To me, the binary-search version is still faster than B, but slower than A.
    Probably because of the closure, but I didn't check.

    --
    Giulio Iotti

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Egon at Sep 6, 2015 at 8:06 am
    Note if you can ensure that the values are always sorted you can use:
    https://play.golang.org/p/_RE6UPndfR

    + Egon
    On Sunday, 6 September 2015 05:12:08 UTC+3, Michael Jones wrote:

    Updated with no change of conclusion:
    http://play.golang.org/p/GdCoVPqRmg

    mbp:append mtj$ go test -v -bench=Merge -benchtime=10s -timeout=9999h
    === RUN TestMergeA
    --- PASS: TestMergeA (0.00s)
    === RUN TestMergeB
    --- PASS: TestMergeB (0.00s)
    PASS
    BenchmarkMergeA10-8 100000000 178 ns/op
    BenchmarkMergeA100-8 3000000 4339 ns/op
    BenchmarkMergeA200-8 1000000 13514 ns/op
    BenchmarkMergeA300-8 500000 28151 ns/op
    BenchmarkMergeA400-8 300000 48820 ns/op
    BenchmarkMergeA500-8 200000 72331 ns/op
    BenchmarkMergeA1000-8 50000 269967 ns/op
    BenchmarkMergeA10000-8 500 26127977 ns/op
    BenchmarkMergeB10-8 10000000 1359 ns/op
    BenchmarkMergeB100-8 1000000 14755 ns/op
    BenchmarkMergeB200-8 500000 30172 ns/op
    BenchmarkMergeB300-8 300000 47152 ns/op
    BenchmarkMergeB400-8 200000 58546 ns/op
    BenchmarkMergeB500-8 200000 84528 ns/op
    BenchmarkMergeB1000-8 100000 170863 ns/op
    BenchmarkMergeB10000-8 10000 1534320 ns/op
    ok append 249.911s


    Michael Jones, CEO • mic...@wearality.com <javascript:> • +1 650
    656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022

    On Sep 5, 2015, at 6:15 PM, Michael Jones <m...@wearality.com
    <javascript:>> wrote:

    just spotted a slight unfairness in method A. does not create a copy so
    values not quite comparable. sorry…I need to retest.


    Michael Jones, CEO • mic...@wearality.com <javascript:> • +1 650
    656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022

    On Sep 5, 2015, at 6:11 PM, Michael Jones <m...@wearality.com
    <javascript:>> wrote:

    [Warning: Pedantry ahead]

    This “easy way to merge” question has already been answered to the OP’s
    satisfaction. But I like to do things several ways and time them just to
    understand how that plays out. Here is another, simple way to merge:

    http://play.golang.org/p/J4S5KG51GX

    and a test suite for it:

    http://play.golang.org/p/OFeAHxYLPf

    This “Method A” is inferior in a big-O sense (being O(len(a)*len(b))
    instead of O(len(a)+len(b)), but it is simple and does not instantiate or
    resize a hashtable. So, the question is, at what size of arrays is it
    slower?

    === RUN TestMergeA
    --- PASS: TestMergeA (0.00s)
    === RUN TestMergeB
    --- PASS: TestMergeB (0.00s)
    PASS
    BenchmarkMergeA10-8 1000000000 62.3 ns/op
    BenchmarkMergeA100-8 10000000 4862 ns/op
    BenchmarkMergeA200-8 3000000 16704 ns/op
    BenchmarkMergeA300-8 1000000 35698 ns/op
    BenchmarkMergeA400-8 1000000 61103 ns/op
    BenchmarkMergeA500-8 500000 92235 ns/op
    BenchmarkMergeA1000-8 200000 314616 ns/op
    BenchmarkMergeA10000-8 2000 27669822 ns/op
    BenchmarkMergeB10-8 30000000 1340 ns/op
    BenchmarkMergeB100-8 3000000 14217 ns/op
    BenchmarkMergeB200-8 2000000 29024 ns/op
    BenchmarkMergeB300-8 1000000 47532 ns/op
    BenchmarkMergeB400-8 1000000 57516 ns/op
    BenchmarkMergeB500-8 500000 85069 ns/op
    BenchmarkMergeB1000-8 300000 158194 ns/op
    BenchmarkMergeB10000-8 30000 1480289 ns/op

    As you can see, the simple loop is faster until len(a)+len(b) nears 800.
    Something to consider when you want to be fast.


    Michael Jones, CEO • mic...@wearality.com <javascript:> • +1 650
    656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022


    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Egon at Sep 6, 2015 at 8:07 am

    On Sunday, 6 September 2015 11:05:58 UTC+3, Egon wrote:
    Note if you can ensure that the values are always sorted you can use:
    https://play.golang.org/p/_RE6UPndfR
    Oh, sorry, forgot that in this case you also didn't want duplicates,
    although minor change will fix it:

    https://play.golang.org/p/EJFR5JssKQ

    + Egon


    + Egon
    On Sunday, 6 September 2015 05:12:08 UTC+3, Michael Jones wrote:

    Updated with no change of conclusion:
    http://play.golang.org/p/GdCoVPqRmg

    mbp:append mtj$ go test -v -bench=Merge -benchtime=10s -timeout=9999h
    === RUN TestMergeA
    --- PASS: TestMergeA (0.00s)
    === RUN TestMergeB
    --- PASS: TestMergeB (0.00s)
    PASS
    BenchmarkMergeA10-8 100000000 178 ns/op
    BenchmarkMergeA100-8 3000000 4339 ns/op
    BenchmarkMergeA200-8 1000000 13514 ns/op
    BenchmarkMergeA300-8 500000 28151 ns/op
    BenchmarkMergeA400-8 300000 48820 ns/op
    BenchmarkMergeA500-8 200000 72331 ns/op
    BenchmarkMergeA1000-8 50000 269967 ns/op
    BenchmarkMergeA10000-8 500 26127977 ns/op
    BenchmarkMergeB10-8 10000000 1359 ns/op
    BenchmarkMergeB100-8 1000000 14755 ns/op
    BenchmarkMergeB200-8 500000 30172 ns/op
    BenchmarkMergeB300-8 300000 47152 ns/op
    BenchmarkMergeB400-8 200000 58546 ns/op
    BenchmarkMergeB500-8 200000 84528 ns/op
    BenchmarkMergeB1000-8 100000 170863 ns/op
    BenchmarkMergeB10000-8 10000 1534320 ns/op
    ok append 249.911s


    Michael Jones, CEO • mic...@wearality.com • +1 650 656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022

    On Sep 5, 2015, at 6:15 PM, Michael Jones wrote:

    just spotted a slight unfairness in method A. does not create a copy so
    values not quite comparable. sorry…I need to retest.


    Michael Jones, CEO • mic...@wearality.com • +1 650 656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022

    On Sep 5, 2015, at 6:11 PM, Michael Jones wrote:

    [Warning: Pedantry ahead]

    This “easy way to merge” question has already been answered to the OP’s
    satisfaction. But I like to do things several ways and time them just to
    understand how that plays out. Here is another, simple way to merge:

    http://play.golang.org/p/J4S5KG51GX

    and a test suite for it:

    http://play.golang.org/p/OFeAHxYLPf

    This “Method A” is inferior in a big-O sense (being O(len(a)*len(b))
    instead of O(len(a)+len(b)), but it is simple and does not instantiate or
    resize a hashtable. So, the question is, at what size of arrays is it
    slower?

    === RUN TestMergeA
    --- PASS: TestMergeA (0.00s)
    === RUN TestMergeB
    --- PASS: TestMergeB (0.00s)
    PASS
    BenchmarkMergeA10-8 1000000000 62.3 ns/op
    BenchmarkMergeA100-8 10000000 4862 ns/op
    BenchmarkMergeA200-8 3000000 16704 ns/op
    BenchmarkMergeA300-8 1000000 35698 ns/op
    BenchmarkMergeA400-8 1000000 61103 ns/op
    BenchmarkMergeA500-8 500000 92235 ns/op
    BenchmarkMergeA1000-8 200000 314616 ns/op
    BenchmarkMergeA10000-8 2000 27669822 ns/op
    BenchmarkMergeB10-8 30000000 1340 ns/op
    BenchmarkMergeB100-8 3000000 14217 ns/op
    BenchmarkMergeB200-8 2000000 29024 ns/op
    BenchmarkMergeB300-8 1000000 47532 ns/op
    BenchmarkMergeB400-8 1000000 57516 ns/op
    BenchmarkMergeB500-8 500000 85069 ns/op
    BenchmarkMergeB1000-8 300000 158194 ns/op
    BenchmarkMergeB10000-8 30000 1480289 ns/op

    As you can see, the simple loop is faster until len(a)+len(b) nears 800.
    Something to consider when you want to be fast.


    Michael Jones, CEO • mic...@wearality.com • +1 650 656-6989
    Wearality Corporation • 289 S. San Antonio Road • Los Altos, CA 94022


    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Kiki Sugiaman at Sep 4, 2015 at 9:07 pm
    2 ways of concatenating:
    http://play.golang.org/p/_zezZr0nhA

    If you want no duplicate, maybe consider using sets?
    https://github.com/deckarep/golang-set
    https://github.com/fatih/set

    On 05/09/15 05:28, Stéphane phenetas wrote:
    Hello,

    I am looking for an easy way to merge or "concatenate" two slices
    together (but without duplicates)

    For example :
    []string{a,b,c,e} + []string{b,c,d} = []string{a,b,c,e,d}


    Thank you in advance.

    --
    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+unsubscribe@googlegroups.com
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Stéphane phenetas at Sep 4, 2015 at 9:52 pm
    Thank you very much to all of you.

    Because order doesn't matter I am implementing the solution given by alexm,
    which sounds like the simplest one.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Martseniuk at Sep 5, 2015 at 7:20 am
    package main

    import (
         "fmt"

         "github.com/zx48/linq"
    )

    func main() {
         a := "aaaa"
         b := "bbb"
         c := "cc"
         d := "dddd"
         e := "eee"

         e1 := linq.FromSlice([]string{a, b, c, e})
         e2 := linq.FromSlice([]string{b, c, d})

         res, err := e1.Concat(e2).Distinct(nil).ToSlice()
         if err != nil {
             fmt.Println(err)
             return
         }
         fmt.Println(res)
    }
    On Friday, September 4, 2015 at 10:28:39 PM UTC+3, Stéphane phenetas wrote:

    Hello,

    I am looking for an easy way to merge or "concatenate" two slices together
    (but without duplicates)

    For example :
    []string{a,b,c,e} + []string{b,c,d} = []string{a,b,c,e,d}


    Thank you in advance.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Chelaruartur at Sep 8, 2015 at 12:17 am
    This look much easier for me :
    str1 := []string{"a", "b", "c"}
    str2 := []string{"d", "e" , "f"}
    str1 = append(str1, str2...)

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Charles Haynes at Sep 8, 2015 at 4:09 am
    Sure, but in the initial posting the requester specified []string{a,b,c,e}
    + []string{b,c,d} = []string{a,b,c,e,d}
    (ignoring order)

    Your solution would give []string{a,b,c,e} + []string{b,c,d} =
    []string{a,b,b,c,c,e,d}

    which is to say the original request asks to have duplicates eliminated and
    append does not do that.

    -- Charles

    On Sat, Sep 5, 2015 at 6:31 PM, wrote:

    This look much easier for me :
    str1 := []string{"a", "b", "c"}
    str2 := []string{"d", "e" , "f"}
    str1 = append(str1, str2...)

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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 4, '15 at 7:28p
activeSep 8, '15 at 4:09a
posts30
users14
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase