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
SQL Statements with Equals VS In
How to Change the Primary Key to Be Non-Clustered
Find the Maximum Consecutive Years for Each Id's in a Table(Oracle SQL)
Left Inner Join VS. Left Outer Join - Why Does the Outer Take Longer
Insert Record to SQL Table with Identity Column
Make H2 Treat Quoted Name and Unquoted Name as the Same
How to Open Bcp Host Data-File
How to Execute a Stored Procedure Over a Set Without Using a Cursor
Select Records in on Table Based on Conditions from Another Table
Deferrable Check Constraint in Postgresql
How to Expand a "Condensed" Postgresql Row into Separate Columns
How to Match Multiple Column in a Table with SQLite Fts3
SQL Group by Day, Show Orders for Each Day
Filter Duplicate Rows Based on a Field
How to Get Age in Years,Months and Days Using Oracle
Rename Single Column in Select * in SQL, Select All But a Column
Row_Number() Over Not Fast Enough with Large Result Set, Any Good Solution
A Constraint That Only Allows One of Two Tables to Reference a Base Table