1

I came across this requirement in Go where I need to take the union of slices of strings

This is for Go version 1.12.5

func Union(a, b []string) []string {
    m := make(map[string]bool)

    for _, item := range a {
        m[item] = true
    }

    for _, item := range b {
        if _, ok := m[item]; !ok {
            a = append(a, item)
        }
    }
    return a
}

I have the above piece of code, but the performance is quite critical here because I need to union many slices altogether. I have to do another for loop to loop over the list of slices. Maybe is there any better way to achieve this?

More info: All the slices are ready in one place and each is sorted in lexicographic order. Almost every slice has duplicates with others.

icza
  • 389,944
  • 63
  • 907
  • 827
Dan Lee
  • 600
  • 6
  • 10
  • Do you have all the slices ready in one place to be "unioned"? Or you must merge 2 at a time? Also are the slices sorted? And what are the chances of they having duplicates? Is that "heuristic" unknown? These are all factors that can be used to improve the performance of the solution. – icza Jul 05 '19 at 07:28
  • All the slices are ready in one place and they are sorted in lexicographic order. Almost every slice has duplicates with others. – Dan Lee Jul 05 '19 at 07:31
  • If you have all the slices sorted, you may do binary search to check if an element is in it without building a map. On the other hand, you may also build a single map, putting all elements in it, using the string values as keys. That solves all the duplicates without having to use binary searches. Which one would be faster, depends on the size of your slices, and the frequency of collisions. You may implement both and benchmark on your input. – icza Jul 05 '19 at 07:41
  • Rather than creating a map for each union of two slices, resulting in the logic being ran N-1 times (where N is the amount of slices you need to union) it would probably be better two create a specialised code to do this just once. – Lucat Jul 05 '19 at 09:34

0 Answers0