Get all subsets of an ArrayList.

I have an ArrayList 'System.Collections.ArrayList' in Powershell which contains a number of numeric items e.g. 5, 10. I am looking for a way I can find all subsets of this array so for the example here it would be...


The order of the sets is not important so set (5, 10) is the same as (10, 5) and am empty set should not count... In reality the array list I have is much larger - any ideas?
Who is Participating?
d-glitchConnect With a Mentor Commented:
If you have a set of N elements, there will be 2^N subsets including the empty set and the entire set.

The standard way to generate them all is to assign each element to one of the digits in an N-digit binary number.  Then count from 0 to (2^N)-1.

So if your set is [ 15 10 5 ]   your subsets would be ...

000     [  ]             <== empty  set
001     [  5 ]
010     [ 10 ]
011     [ 10 5 ]
100     [  15 ]
111     [  15 10 5 ]   <==  entire set

Note that this works with duplicate elements as well.
Blowfelt82Author Commented:
It also may be important to note that the list may at some point have duplicate items - this is not the case at the moment but would be good to future proof any solution?
Meir RivkinFull stack Software EngineerCommented:
what do u mean subsets of this array?
Managed Security Services Webinar - March 15

Selecting the right managed security services platform to grow your business can be a huge undertaking. Join WatchGuard and Frost & Sullivan in an upcoming webinar as we dive into the key elements of selecting a vendor platform and partnership to fuel a successful MSSP business.

Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
Please post atleast 10 items in the array please so that we can get clear idea what the problem is.
>>  Note that this works with duplicate elements as well.

Actually, duplicate elements may be a little tricky.

If your set contains duplicates, presumably the subsets can as well.
But the binary counting will generate multiple copies of some subsets.
Suppose your set consists of N items (no duplicates) plus D copies of another item.

The total number of subsets would be  (2^N)*(D+1)

You would generate the subsets of the N unique items as mentioned earlier.
Then you would add 0, 1, 2, ... N copies of the duplicated items.

The mathematics always includes the empty set. You can discard if you wish.

You can extend the math and generation procedure for multiple duplicated items.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.