Array Combinations

Photo by Ricardo Gomez Angel on Unsplash

Array Combinations

You will be given an array of arrays...

Katie Marie - Among the Branches's photo
Katie Marie - Among the Branches
·Jan 23, 2022·

4 min read

CHALLENGE

Array Combinations

Instructions: In this Kata, you will be 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.

See test cases for more examples.

Good luck!

Need a refresher of the PREP method? How about a quick array methods quiz?

PARAMETERS

Arrays or subarrays of numbers (positive only, no gotchas)

The number (count) of unique arrays that can be formed by picking just one element from each array.

EXAMPLES & TESTS:

solve([[1,2],[4],[5,6]]) = 4 because it results in only 4 possibilities. They are [1,4,5],[1,4,6],[2,4,5],[2,4,6]. Don’t count duplicates.

solve([[1,2],[4],[5,6]]),4); solve([[1,2],[4,4],[5,6,6]]),4); solve([[1,2],[3,4],[5,6]]),8); solve([[1,2,3],[3,4,6,6,7],[8,9,10,12,5,6]]),72);

PSEUDOCODE

const solve = arr


console.log(solve([[1,2],[4],[5,6]]),4);
console.log(solve([[1,2],[4,4],[5,6,6]]),4);
console.log(solve([[1,2],[3,4],[5,6]]),8);
console.log(solve([[1,2,3],[3,4,6,6,7],[8,9,10,12,5,6]]),72);
  • const solve = arr I’m passing in an array, and that array has subarrays. So I’m going to use arr.map(). This is all about turning the array into Sets, then, the map should eventually give me an array that looks like that (3,4,6), then we're going to reduce that array.

  • Going to use .map, because I want to go through the entire array grabbing each of the elements. Map is going to call in a function.

  • Map is looping through each of these subarrays. If I’m looking at test #3, x is first the [1,2] array. Then x will be [3,4]. Then x will be [5,6].
  • Since I know x is the subarray, this little bit of code here is giving me a new set:
 [...new Set (x)]
  • What I’ll end up with is an array that does not have duplicates.
  • Let’s say we have [2, 3, 3, 3, 5, 7] - this becomes x. If I plug that array in for x, what would wind up happening is that this new set would return [2,3,5,7], and length would be 4. So now this map is creating a new array, the first value in that new array would be 4. Then again for the next subarray, would be 2.

  • As we map through these sets, we just wind up with how many unique numbers were inside that subarray. So once I have this map all done to completion, and we’ve gone through figuring out how many items are in each of those subarrays, we .reduce() it.

  • Reduce - lets say we’re reducing in Test #4. [3,4,6]
  • What it’s going to do - the a, accumulator is the variable where you’re storing stuff. And c is current value. Many people write acc/current but it doesn’t matter what you call it.
  • Reduce is going to take the current value and multiply it by the accumulator. We assume the accumulation is 1. 1x3 = 3, reduce will run again but this time the accumulation is already at 3… 3x4=12, now accumulator is 12… 12x5=60. So the whole line of code (in solution below) just reduces to 60.

SOLUTION:

const solve = arr => arr.map( x => [...new Set(x)].length ).reduce((a,c)=>a*c)

Remember, for an extra Codewars point, give the challange a thumbs-up after submitting!

NOTES:

  • SET is almost like an array but you can’t have duplicates. Doesn’t allow duplicates, kinda gets rid of it.
  • Map is an array method that returns an array.
  • Chaining Methods: as long as it’s returning a value that the next thing utilizes, you can chain methods to your heart’s content.You can chain a map and a reduce because they’re both working on an array. You can’t chain something like “Bob” and map because "Bob" is a string.

ADDITIONAL RESOURCES:

Map MDN

Reduce MDN

Set MDN

ASK YOURSELF: WHAT DO I NEED TO REVIEW? WHERE DID I GET STUCK?

Then go back and do it again. Talk through it step by step.

Then, add the problem to your ANKI deck, and come back to it again in regular intervals.

Learn about the benefits of active recall

You can do this!

 
Share this