I need the classic N choose K algorithm to generate all the possible combinations of a list or iterable in Dart. Is there any implementation out there?
Asked
Active
Viewed 2,299 times
5
-
Something like https://pub.dartlang.org/packages/trotter? – Günter Zöchbauer Mar 31 '15 at 19:03
-
Sounds perfect! Why don't you post the answer? – Cristian Garcia Apr 06 '15 at 15:26
2 Answers
7
The package trotter seems to do what you are looking for.

Günter Zöchbauer
- 623,577
- 216
- 2,003
- 1,567
-
3even through it looks cool, it doesn't have null safety as for Sept 1 2021 – goodniceweb Sep 01 '21 at 06:39
-
1
-
1@goodniceweb checkout this solution https://stackoverflow.com/a/64302098/12695188 – genericUser Mar 18 '22 at 11:30
2
There is now an unreleased null safety version of trotter. Add the following dependency to your pubspec.yaml:
trotter: ^2.0.0-dev.1
Then you can do something like this:
var c = Combinations(3, characters('abcd'));
print('There are ${c.length} 3-combinations of the objects');
print('in ${c.items}.');
print('The first combination is ${c[0]}.');
You can make a Combinations object for a custom type as well:
var c = Combinations<MyType>(3, [obj1, obj2]);

Mike Knapp
- 71
- 4