I am trying to create a subset of a list, covering every possible combination with the condition that final output is the same length as the initial list and there are no repeating elements.
For the list:
X <- c("A","B","C","D")
All the non-null subsets are (let's call it Y
):
[('A'), ('B'), ('C'), ('D'), ('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'),
('B', 'D'), ('C', 'D'), ('A', 'B', 'C'), ('A', 'B', 'D'), ('A', 'C', 'D'),
('B', 'C', 'D'), ('A', 'B', 'C', 'D')]
What I am looking for is combinations of Y
such that the elements within the combination are distinct values of X
.
Some of the acceptable combinations would be:
(('A',), ('B',), ('C', 'D'))
(('A',), ('C',), ('B', 'D'))
(('A',), ('D',), ('B', 'C'))
(('B',), ('C',), ('A', 'D'))
(('B',), ('D',), ('A', 'C'))
(('C',), ('D',), ('A', 'B'))
I have tried estimating all possible combinations of Y
and then getting the length of the distinct values of each combination.
If the length(distinct elements of combination) = length(X)
then I keep the combination. But this isn't an optimal method by any means and does not cover repeating scenarios.
Also, in my real world scenario, I have up to 40 distinct elements in X
.