Array Combinations Without Repetition

Combination without repetition javascript

this was quite complex, and lots of fun, so thanks for asking!

as here we can have m element arrays of arbitary size,

var a = [  ["blue", "red"],  [1, 2],  [true, false],]
function allPossibleCombinations(items, isCombination=false){ // finding all possible combinations of the last 2 items // remove those 2, add these combinations // isCombination shows if the last element is itself part of the combination series if(items.length == 1){ return items[0] } else if(items.length == 2){ var combinations = [] for (var i=0; i<items[1].length; i++){ for(var j=0; j<items[0].length; j++){ if(isCombination){ // clone array to not modify original array var combination = items[1][i].slice(); combination.push(items[0][j]); } else{ var combination = [items[1][i], items[0][j]]; } combinations.push(combination); } } return combinations } else if(items.length > 2){ var last2 = items.slice(-2); var butLast2 = items.slice(0, items.length - 2); last2 = allPossibleCombinations(last2, isCombination); butLast2.push(last2) var combinations = butLast2; return allPossibleCombinations(combinations, isCombination=true) }}
console.log(allPossibleCombinations(a));console.log(allPossibleCombinations(a).length);

Get all possible combination of items in array without duplicate groups in Swift

You can use flatMap also to combine them in one line.

extension Array {
var combinationsWithoutRepetition: [[Element]] {
guard !isEmpty else { return [[]] }
return Array(self[1...]).combinationsWithoutRepetition.flatMap { [$0, [self[0]] + $0] }
}
}

print([1,2,3,4].combinationsWithoutRepetition)

All possible combinations of an array without repeating?

What you want aren't all possible combinations (subsets, missing are 1,3, 1,4, 2,4), but all subsequences. You can get those easily by using two nested loops for start and end of the sequence:

function subsequences(arr) {
var res = [[]];
for (var i=0; i<arr.length; i++)
for (var j=i+1; j<=arr.length; j++)
res.push(arr.slice(i, j));
return res;
}

For all possible subsets - the power set - see this answer.

Python combinations without repetitions

As Donkey Kong points to set, You can get the unique values in a list by converting the list to a set :

t = [2,2,2,2,4]
c = list(itertools.combinations(t, 4))
unq = set(c)
print(unq)

And the result will be:

{(2, 2, 2, 4), (2, 2, 2, 2)}

If you want to use it as a list, you can convert it back by doing :

result = list(unq)

Alternative and more clean,comprehensive way will be :

t = [2,2,2,2,4]
c = set(itertools.combinations(t, 4))

Find if an array is a combination without repetition

If I understand your problem correctly, this method basically checks both source and sub array have common elements and those size of common elements should be equal to the sub array. You can do the check with array intersection.

source = [:a, :b, :c, :d, :e]
a1 = [:e, :c, :b]
a2 = [:e, :c, :e]
a3 = [:e, :b, :k]

(source & a1).size == a1.size
=> true
(source & a2).size == a2.size
=> false
(source & a3).size == a3.size
=> false

Random combinations from different arrays with not repetition

You can identify each combination with a unique number, and maintain a set of used numbers:

function choices(count, ...arrays) {
let used = new Set;
let size = arrays.reduce((size, arr) => size * arr.length, 1);
if (count > size) count = size;
let result = [];
for (let i = 0; i < count; i++) {
let k;
do {
k = Math.floor(Math.random() * size);
} while (used.has(k));
used.add(k);
result.push(arrays.reduce((acc, arr) => {
acc.push(arr[k % arr.length]);
k = Math.floor(k / arr.length);
return acc;
}, []));
}
return result;
}

let accesories = ["a", "b", "c", "d", "e"];
let hats = ["A", "B", "C", "D", "E"];
let tshirts = ["1", "2", "3", "4", "5"];
let boots = [".", ";", "?", "!"];

// Get 10 choices:
let result = choices(10, accesories, hats, tshirts, boots);
console.log(result);


Related Topics



Leave a reply



Submit