Thursday, September 21, 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 job will likely be to return the variety of distinctive arrays that may be shaped by choosing precisely one ingredient from every subarray.

For instance: `remedy([[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 depend duplicates; for instance `remedy([[1,2],[4,4],[5,6,6]]) = 4`, for the reason that additional outcomes are simply duplicates.

Choice 1:

``````bundle resolution
func Resolve(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
}
``````

Choice 2:

``````bundle resolution
func Resolve(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
}
``````

Choice 3:

``````bundle resolution
func Resolve(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
}
}
end result := 1
for _, s := vary units {
end result *= len(s)
}
return end result
}
``````

## 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 assessments", func() {
Anticipate(Resolve([][]int{{1, 2}, {4}, {5, 6}})).To(Equal(4))
Anticipate(Resolve([][]int{{1, 2}, {4, 4}, {5, 6, 6}})).To(Equal(4))
Anticipate(Resolve([][]int{{1, 2}, {3, 4}, {5, 6}})).To(Equal(8))
Anticipate(Resolve([][]int{{1, 2, 3}, {3, 4, 6, 6, 7}, {8, 9, 10, 12, 5, 6}})).To(Equal(72))
})
})
``````
RELATED ARTICLES