Skip to content

Any way to handle duplicates? #56

@barbazul

Description

@barbazul

I have a set of elements with possible duplication.

I need to get all possible permutations of combinations but removing duplicates.

Like so:

const all = ['a', 'a', 'b'];
const cmb = Combinatorics.permutationCombination(all);
console.log(cmb.toArray());

Expected output is:

[ [ 'a' ],
  [ 'b' ],
  [ 'a', 'a' ],
  [ 'a', 'b' ],
  [ 'b', 'a' ],
  [ 'a', 'a', 'b' ],
  [ 'a', 'b', 'a' ],
  [ 'b', 'a', 'a' ] ]

but instead, I am getting

[ [ 'a' ],
  [ 'a' ],
  [ 'b' ],
  [ 'a', 'a' ],
  [ 'a', 'a' ],
  [ 'a', 'b' ],
  [ 'b', 'a' ],
  [ 'a', 'b' ],
  [ 'b', 'a' ],
  [ 'a', 'a', 'b' ],
  [ 'a', 'b', 'a' ],
  [ 'a', 'a', 'b' ],
  [ 'a', 'b', 'a' ],
  [ 'b', 'a', 'a' ],
  [ 'b', 'a', 'a' ] ]

So far I have been able to work around this with a control list and checking while iterating. I could refactor this into a lazyFilter that checks such list, but it would pretty much be the same as I still need to externally keep track of the already visited permutations.

Any way to keep this contained within the library?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions