Here's the code to get distinct permutations of a set in Swift using recursion:
main.swift525 chars25 lines
The above function uses Heap's algorithm to generate permutations recursively. The function takes a set as input and returns an array of distinct permutations. The heapPermutation
function generates the permutations by swapping elements in the set, and the base case is reached when there is only one element left.
To use this function, simply call getPermutations
and pass in a set of elements:
main.swift163 chars4 lines
gistlibby LogSnag