# Forming Unique Array Combinations in Golang

## The challenge

You are given an array of arrays and your task will be to return the number of unique arrays that can be formed by picking exactly one element from each subarray.

For example: `solve([[1,2],[4],[5,6]]) = 4`, because it results in only `4` possibilites. They are `[1,4,5],[1,4,6],[2,4,5],[2,4,6]`.

Make sure that you don’t count duplicates; for example `solve([[1,2],[4,4],[5,6,6]]) = 4`, since the extra outcomes are just duplicates.

## The solution in Golang

Option 1:

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```package solution
func Solve(data [][]int) int {
qtd := 1
for _, sss := range data {
mp := make(map[int]bool)
for _, e := range sss {
mp[e] = true
}
qtd *= len(mp)
}
return qtd
}
```Code language: Go (go)```

Option 2:

``````package solution
func Solve(data [][]int) int {
res := 1
for _, d := range data{
cnt := 0
dup := make(map[int]int)
for _, n := range d {
if dup[n] == 0 {
cnt++
dup[n] = 1
}
}
res *= cnt
}
return res
}
```Code language: Go (go)```

Option 3:

``````package solution
func Solve(data [][]int) int {
sets := make([]map[int]bool, len(data))
for i := 0; i < len(data); i++ {
sets[i] = make(map[int]bool, len(data[i]))
for _, v := range data[i] {
sets[i][v] = true
}
}
result := 1
for _, s := range sets {
result *= len(s)
}
return result
}
```Code language: Go (go)```

## Test cases to validate our solution

``````package solution_test
import (
"math/rand"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
var _ = Describe("Sample Tests", func() {
It("should work with sample tests", func() {
Expect(Solve([][]int{{1, 2}, {4}, {5, 6}})).To(Equal(4))
Expect(Solve([][]int{{1, 2}, {4, 4}, {5, 6, 6}})).To(Equal(4))
Expect(Solve([][]int{{1, 2}, {3, 4}, {5, 6}})).To(Equal(8))
Expect(Solve([][]int{{1, 2, 3}, {3, 4, 6, 6, 7}, {8, 9, 10, 12, 5, 6}})).To(Equal(72))
})
})
```Code language: Go (go)```
Tags:
Subscribe
Notify of