The problem
You might be given an array of arrays and your activity might be to return the variety of distinctive arrays that may be shaped by selecting precisely one ingredient from every subarray.
For instance: resolve([[1,2],[4],[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 remember to don’t depend duplicates; for instance resolve([[1,2],[4,4],[5,6,6]]) = 4
, for the reason that further outcomes are simply duplicates.
The answer in Golang
Choice 1:
bundle answer
func Clear up(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 answer
func Clear up(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 answer
func Clear up(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
}
Check instances to validate our answer
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 exams", func() {
Anticipate(Clear up([][]int{{1, 2}, {4}, {5, 6}})).To(Equal(4))
Anticipate(Clear up([][]int{{1, 2}, {4, 4}, {5, 6, 6}})).To(Equal(4))
Anticipate(Clear up([][]int{{1, 2}, {3, 4}, {5, 6}})).To(Equal(8))
Anticipate(Clear up([][]int{{1, 2, 3}, {3, 4, 6, 6, 7}, {8, 9, 10, 12, 5, 6}})).To(Equal(72))
})
})