# How to find all partitions of a multiset, where each part has distinct elements in JavaScript

Let's say we have such an array −

const arr = [A, A, B, B, C, C, D, E];

We are required to create an algorithm so that it will find all the combinations that add up to the whole array, where none of the elements are repeated.

Example combinations −

[A, B, C, D, E] [A, B, C]
[A, B, C, D] [A, B, C, E]
[A, B, C] [A, B, C] [D, E]

## Explanation

[A, B, C] [A, B, C] [D, E] and [A, B, C] [D, E] [A, B, C] are the same combinations. Also, the ordering with the subsets doesn't matter as well.

For example − [A,B,C] and [B,A,C] should be the same.

## Example

The code for this will be −

const arr = [['A', 1], ['B', 2], ['C', 3]];
const spread = (arr, ind, combination) => {
if (arr === 0)
return [combination];
if (ind === −1)
return [combination.concat([arr])];
let result = [];
for (let c=1; c<=Math.min(combination[ind], arr); c++){
let comb = combination.map(x => x.slice());
if (c == comb[ind]){
comb[ind] += arr;
} else {
comb[ind] −= c;
comb.push([comb[ind] + arr, c]);
}
result = result.concat(spread([arr, arr − c], ind − 1, comb));
}
let comb = combination.map(x => x.slice());
return result.concat(spread(arr, ind − 1, comb));
};
const helper = arr => {
function inner(ind){
if (ind === 0)
return [[arr]];
const combs = inner(ind − 1);
let result = [];
for (let comb of combs)
result = result.concat(
return result;
}
return inner(arr.length − 1);
};
const returnPattern = (arr = []) => {
const rs = helper(arr);
const set = new Set();
for (let r of rs){
const _r = JSON.stringify(r);
if (set.has(_r))
console.log('Duplicate: ' + _r);
}
let str = '';
for (let r of set)
str += '' + r
str += '';
return str;
};
console.log(returnPattern(arr));

## Output

And the output in the console will be −

[["ABC",1],["BC",1],["C",1]]
[["AB",1],["BC",1],["C",2]]
[["ABC",1],["B",1],["C",2]]
[["AB",1],["B",1],["C",3]]
[["AC",1],["B",1],["BC",1],["C",1]]
[["A",1],["B",1],["BC",1],["C",2]]
[["AC",1],["BC",2]]
[["A",1],["BC",2],["C",1]]
[["AC",1],["B",2],["C",2]]
[["A",1],["B",2],["C",3]]