Array Combinations

CHALLENGE

Array Combinations

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!

Ok, don’t panic. The instructions sound like a lot, but we can break it down using the PREP method:

Parameters

  1. Array of subarrays of numbers (positive only, no gotchas) 

Returns

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

Examples

  1. Example: 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.
  1. Test.assertEquals(solve([[1,2],[4],[5,6]]),4);
  2. Test.assertEquals(solve([[1,2],[4,4],[5,6,6]]),4);
  3. Test.assertEquals(solve([[1,2],[3,4],[5,6]]),8);
  4. Test.assertEquals(solve([[1,2,3],[3,4,6,6,7],[8,9,10,12,5,6]]),72);

Pseudocode (talking through the problem, step by step):

  • I’m passing in an array, and that array has subarrays. 
  • I want to go through the entire array grabbing each of the elements. (alarm bells for map()!) 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 we 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 to a single number.  Example: in test #4, it’s going to do 3×1=3, + 3×4=12 + 3×6=18, = 72 . [3,4,6] The a, accumulator is the variable where you’re storing stuff. And c is current value.  It can also be written as acc/current – but it doesn’t matter what you call it.  For example: Test #4 is going to do 3×1=3, + 3×4=12 + 3×6=18, = 72. Reduce() is going to take the current value and multiply it by the accumulatorWe assume the accumulation is 1.  1×3 = 3, reduce will run again but this time the accumulation is already at 3… 3×4=12, now accumulator is 12… 12×5=60.  So that 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)

Further Exploration:

  1. Map MDN
  2. Reduce MDN
  3. Set MDN

Leave a Comment