uniquePermutations(ofCount:)
Returns a collection of the unique permutations of this sequence with lengths in the specified range.
func uniquePermutations<R>(ofCount kRange: R) -> UniquePermutationsSequence<Self> where R : RangeExpression, R.Bound == Int
Parameters
- kRange
A range of the number of elements to include in each permutation.
kRange
can be any integer range expression, and is clamped to the number of elements in this collection. Passing a range covering sizes greater than the number of elements in this collection results in an empty sequence.
Use this method to iterate over the unique permutations of a sequence with repeating elements. This example prints every unique permutation of an array of numbers with lengths through 2 elements:
let numbers = [1, 1, 2]
for perm in numbers.uniquePermutations(ofCount: ...2) {
print(perm)
}
// []
// [1]
// [2]
// [1, 1]
// [1, 2]
// [2, 1]
The returned permutations are in ascending order by length, and then lexicographically within each group of the same length.