Home > Mobile >  Javascript Unique Combinations with two Arrays
Javascript Unique Combinations with two Arrays

Time:05-03

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; 
}

CodePudding user response:

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 and use the binary representation of the counter to select the elements either from first or from second array:

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

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

CodePudding user response:

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.

  • Related