'Javascript Unique Combinations with two Arrays
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;
}
Solution 1:[1]
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.
Solution 2:[2]
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);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |