Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get all subsets of an ArrayList.

Posted on 2013-05-20
6
Medium Priority
?
399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Meir Rivkin
ID: 39180369
what do u mean subsets of this array?
0
 
LVL 19

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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 27

Accepted Solution

by:
d-glitch earned 2000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

604 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