I have an array of numbers. In this array each number is repeating for "r" times.
This function is generating the array:
var n = 8;
var k = 4;
var r = 3;
var comb = n * r / k;
function getNumbers() {
var array = new Array();
for (i = 0; i < r; i++) {
for (j = 0; j < n; j++) {
array.push(j);
}
}
return array;
}
The numbers from this array I want to split them in "comb=n*r/k" uniq small arrays with the length of k;
for this I made the following function:
function InsertNumber(array) {
var finalarray = GetArrays(comb);
for (j = 0; j < array.length; j++) {
for (i = 0; i < finalarray.length; i++) {
if (ContainX(array[j], finalarray[i])) {
if (finalarray[i].length <= k) {
finalarray[i].push(array[j]);
Console.log(array[j]);
var index = array.indexOf(array[j]);
array.splice(index, 1);
InserNumber(array);
}
}
}
}
ShowTable(finalarray);
}
function GetArrays(x) {
var array = new Array();
for (i = 0; i < x; i++) {
var smallArray= new Array();
array.push(smallArray);
}
return array;
}
function ContainX(array,element) {
var result = false;
for (i = 0; i < array.length; i++) {
if (element === array[i]) {
result = true;
}
}
return result;
}
finaly I want to display all the small arays items in a table using this function:
function ShowTable(array) {
document.write("<table>")
for (i = 0; i < array.lenght; i++) {
document.write("<tr>")
for (j = 0; j < array[i].legth; j++) {
document.write("<td>" + array[i][j] + "</td>")
}
document.write("</tr>")
}
document.write("</table>")
}
I think that the step by step algorithm to get the expected result may be ok, but I am not able to see the result in browser because of the recursive function InsertNumber().
Can you help me with a better method to generate all combinations of all numbers in an array, where the the array items may repeat for r times?
I am open for any solution which can fix my issue.
Edit:
Exemple: mainArray=[0,0,1,1,2,2]
;
I want to split this array in:
arr1=[0,1];
arr2=[0,2];
arr3=[1,2];
this 3 arrays are containing all items of mainArray and are uniq!!!.
In this exemple: n=3, k=2, r=2, comb=n*r/k=3;
n=total unique numbers from `mainArray [0,1,2]`;
k=length of small arrays
r= number of repetition of each unique n;
comb= the number of small arrays;
**Edit2- Exemple2:**
mainArray=[0,0,1,1,2,2,3,3,4,4]
arr1=[0,1];
arr2=[0,2];
arr3=[1,3];
arr4=[2,4];
arr5=[3,4];
n=5, unique numbers in main array;
k=2, length of small array;
r=2, repetition of each number in main array;
comb=5*2/2=number of combinations which must result!.
(all the items from mainArray are spllited in small arr is such a way to not have small array with same items)