1

I am given 2 arrays , each of them has 10 elements each as shown below: [A1, B1, C1, D1, E1, F1, G1, H1, I1, J1] and [A2, B2, C2, D2, E2, F2, G2, H2, I2, J2]

I am expected to do the following:

list all possible combinations of the elements of the first two arrays making sure that the occurence of an element of the same index does not occur per output ie A1 and A2 should not be appear on same output same as B1 and B2 etc

Hint: Output should look like this: [

  [A1, B1, C1, D1, E1, F1, G1, H1, I1, J1]
  [A2, B2, C2, D2, E2, F2, G2, H2, I2, J2]
  [A1, B2, C2, D2, E2, F2, G2, H2, I2, J2]
  [A2, B1, C1, D1, E1, F1, G1, H1, I1, J1]
  [A1, B1, C1, D1, E1, F1, G1, H1, I1, J2]      
  ...     
  ...   
  ...
  [A1, B1, C2, D2, E2, F2, G2, H2, I2, J2]
  [A1, B1, C1, D2, E1, F1, G1, H1, I1, J2]
  [A1, B1, C1, D1, E2, F1, G1, H1, I1, J2]
  ...
  ...
  ...
]

There are supposed to be 1024 outputs in all

i need help to achieve the above output in Javascript

below is waht I have tried:

function combineLatest1(arrFirst,arrSecond){
  let newArr = [[...arrFirst],[...arrSecond]];  // make copies of the original arrays since they are valid output   
  // perform the looping over the two arrays
  arrFirst.shift();
  let tempAr = [...arrFirst];
  arrSecond.forEach(function(item){
     if(!tempAr.includes(item)){
        tempAr.push(item);  
     } 
     newArr.push(tempAr);
  });
  
  return newArr; 
}

2 Answers 2

1

I have to admit that I don't understand your approach and can't fix it, but I can show you a different approach.

You can iterate from 0 to 1023 (number of arrays ** elements in each array - 1) and use the modulus operator with the counter to select the elements from one of the arrays. This solution works with an arbitrary number of arrays of same length.

const arrays = [['A1', 'B1', 'C1', 'D1', 'E1', 'F1', 'G1', 'H1', 'I1', 'J1'], ['A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2', 'H2', 'I2', 'J2']];

// iterate over all combinations
for (let i = 0, combinations = arrays.length ** arrays[0].length; i < combinations; ++i) {
  // result array
  const arr = [];
  // iterate over elements in the arrays
  // k is helper variable to select the input array
  for (let j = 0, k = i, elements = arrays[0].length; j < elements; ++j, k = Math.floor(k / arrays.length)) {
    // select element by binary representation
    arr.push(arrays[k % arrays.length][j]);
  }
  console.log(arr);
}

Example with 3 arrays:

const arrays = [['A1', 'B1', 'C1', 'D1', 'E1', 'F1', 'G1', 'H1', 'I1', 'J1'], ['A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2', 'H2', 'I2', 'J2'], ['A3', 'B3', 'C3', 'D3', 'E3', 'F3', 'G3', 'H3', 'I3', 'J3']];

for (let i = 0, combinations = arrays.length ** arrays[0].length; i < combinations; ++i) {
  const arr = [];
  for (let j = 0, k = i, elements = arrays[0].length; j < elements; ++j, k = Math.floor(k / arrays.length)) {
    arr.push(arrays[k % arrays.length][j]);
  }
  console.log(arr);
}

Sign up to request clarification or add additional context in comments.

6 Comments

great solution but what would this code look like when the number of arrays is increased. let's say there are three arrays now eg: const a3 = ['A3', 'B3', 'C3', 'D3', 'E3', 'F3', 'G3', 'H3', 'I3', 'J3']; with which there are supposed to be 59,049 outputs
@DidigwuNnaemekaChristian This approach doesn't work with 3 arrays. It's based on the binary number representation and uses each digit as a switch to select an array. A digit can only select between two options. Maybe you can replace the 2 with the number of arrays, but I'm not sure if it will work.
let resultArr =[]; for (let i = 0; i < len2; ++i) { let k = i; const arr = []; for (let j = 0; j < ar3.length; ++j) { let y = k % 3; switch(y){ case 0: arr.push(ar3[j]); break; case 1: arr.push(ar2[j]); break; case 2: arr.push(ar1[j]); break; } k = Math.floor(k / 3); } //console.log(arr); resultArr.push(arr); } return resultArr; } I did the above and it worked for me
@DidigwuNnaemekaChristian I modified my code to support an arbitrary number of arrays. I really only had to replace the 2 with the number of arrays.
That's powrfull --@jabaa that's a great solution. in the case of solution I FIXED the number of arrays but yours is a better solution since it is more dynamic to accomodate any numbermof arrays under consideration
|
0

I've made a working snippet illustrating an approach that builds the required array 'from scratch' based on your description.

It relies on string representations of binary numbers in the decimal range 0-1024. Each is padded with leading empty bits (0) before each bit is incremented by 1 e.g. 010111111 would become 121222222. This is then divided into characters to use as the numeric part of the required cell reference. The letter components are built in the same loop using ascII values to specify the letter.

The example only outputs the first 32 lines, change 32 for 1024 in the outer loop to get the entire set.

const collection = [];

for (let i=0; i<32; i++) {

let binString = i.toString(2)

let paddedBin = "0".repeat(10 - binString.length)+binString;

const bits = paddedBin.split("");


labels = bits.map(element => parseInt(element)+1);

const currentArray = [];
for (let letter=0; letter<10; letter++) {
currentArray.push(`${String.fromCharCode(letter+65)}${labels[letter]}`)
}

collection.push(currentArray)
} 

console.log(collection);

If you have to have the final array in the order shown in your example, an array sort function will need to be built to reorder the lines.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.