Sunday, February 19, 2023
HomeSoftware EngineeringForming Distinctive Array Combos in Golang

# Forming Distinctive Array Combos in Golang

## The problem#

You might be given an array of arrays and your process shall be to return the variety of distinctive arrays that may be fashioned by selecting precisely one component from every subarray.

For instance: `clear up([[1,2],,[5,6]]) = 4`, as a result of it leads to solely `4` possibilites. They’re `[1,4,5],[1,4,6],[2,4,5],[2,4,6]`.

Just be sure you don’t rely duplicates; for instance `clear up([[1,2],[4,4],[5,6,6]]) = 4`, for the reason that additional outcomes are simply duplicates.

Possibility 1:

``````bundle resolution
func Remedy(information [][]int) int {
qtd := 1
for _, sss := vary information {
mp := make(map[int]bool)
for _, e := vary sss {
mp[e] = true
}
qtd *= len(mp)
}
return qtd
}
``````

Possibility 2:

``````bundle resolution
func Remedy(information [][]int) int {
res := 1
for _, d := vary information{
cnt := 0
dup := make(map[int]int)
for _, n := vary d {
if dup[n] == 0 {
cnt++
dup[n] = 1
}
}
res *= cnt
}
return res
}
``````

Possibility 3:

``````bundle resolution
func Remedy(information [][]int) int {
units := make([]map[int]bool, len(information))
for i := 0; i < len(information); i++ {
units[i] = make(map[int]bool, len(information[i]))
for _, v := vary information[i] {
units[i][v] = true
}
}
outcome := 1
for _, s := vary units {
outcome *= len(s)
}
return outcome
}
``````

## Take a look at circumstances to validate our resolution#

``````bundle solution_test
import (
"math/rand"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
var _ = Describe("Pattern Exams", func() {
It("ought to work with pattern checks", func() {
Count on(Remedy([][]int{{1, 2}, {4}, {5, 6}})).To(Equal(4))
Count on(Remedy([][]int{{1, 2}, {4, 4}, {5, 6, 6}})).To(Equal(4))
Count on(Remedy([][]int{{1, 2}, {3, 4}, {5, 6}})).To(Equal(8))
Count on(Remedy([][]int{{1, 2, 3}, {3, 4, 6, 6, 7}, {8, 9, 10, 12, 5, 6}})).To(Equal(72))
})
})
``````

RELATED ARTICLES