Solved

Get all subsets of an ArrayList.

Posted on 2013-05-20
6
369 Views
Last Modified: 2013-07-04
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...

5,
10
5,10

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?
0
Comment
Question by:Blowfelt82
6 Comments
 

Author Comment

by:Blowfelt82
ID: 39180355
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?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39180369
what do u mean subsets of this array?
0
 
LVL 18

Expert Comment

by:Raheman M. Abdul
ID: 39180507
Please post atleast 10 items in the array please so that we can get clear idea what the problem is.
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 27

Accepted Solution

by:
d-glitch earned 500 total points
ID: 39181064
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.
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 39181991
>>  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.
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 39184356
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.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now