Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7682181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:30:31+00:00 2026-05-31T18:30:31+00:00

I have a listbox in C# with the following items: Package1 Package2 Package3 Package4

  • 0

I have a listbox in C# with the following items:

Package1
Package2
Package3
Package4
Package5
and so on...

The user can select multiple items from this listbox. I need an algorithm in c# or Java (preferably in c#) that can tell me all the possible selections that a user can do, for example Package1 and Package2, Package3 and Package1, Package2, Package4 and Package 3, etc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:30:32+00:00Added an answer on May 31, 2026 at 6:30 pm

    You can use recursion, “guess” if the first package is in the combination or not – and invoke recursively without the first package.

    Pseudo-code:

    combinations(packages, sol):
       if (packages.length == 0): //base clause
          print sol
          return
       package <- packages.first //1 possibility: the first package is in the combination
       packages.removeFirst()
       sol.append(package) 
       combinations(packages,sol) //recursively invoke without the first package
       sol.removeLast() //clean up environment - remove the last added package
       combinations(packages,sol) //2nd possibility: the first package is not in the combination
    

    Notes:

    • The empty solution [no package was selected] is assumed to be also a feasible option in this algorithm – if it is not, it can be handled easily in the base clause.
    • It is assumed that the order is not important. Package1 AND Package2 is the same as Package2 AND Package1.
    • There are 2^n possible combinations for chosing packages, so any algorithm that do what you ask for [including this one] will need O(2^n) time, so I would not try to invoke this algorithm with 100 packages….

    In java, it could look something like this:

    public static void printCombinations(LinkedList<String> packages, LinkedList<String> sol) {
        if (packages.size() == 0) { 
            System.out.println(sol);
            return;
        }
        String pack = packages.poll(); //also removes the head
        sol.addLast(pack);
        printCombinations(new LinkedList(packages),sol);
        sol.removeLast();
        printCombinations(new LinkedList(packages),sol);
    }
    

    and invokation is by:

    printCombinations(packages, new LinkedList<String>());
    

    Where packages is a LinkedList with all your packages.

    • I used LinkedList for packages and created new objects because I
      find it more clear. To enhance performance – you can use a single
      array with index [which will be changed between recursive calls] in
      order to avoid duplicating packages so many times.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user control with the following XAML: <ScrollViewer> <ListBox ItemsSource={Binding Items}> <ListBox.ItemTemplate>
I have a ListBox that can contain hundreds of items. I've added the following
I have the following code: ListBox.DataSource = DataSet.Tables(table_name).Select(some_criteria = match) ListBox.DisplayMember = name The
I have the following data template for a list box items: <DataTemplate x:Key=substanceListShower> <ListBox
I want to display a listbox containng list items, I have the following template
I have a listbox that has the following items and values right now. Items,
I have a ListBox showing items using the following DataTemplate: <DataTemplate x:Key=PersonTemplate DataType={x:Type DAL:ResultItem}
I have a databound ListBox with an ItemTemplate, following this example : <ListBox ItemsSource={Binding
My environment is windows phone 7.1. I have the following code: <ListBox ItemsSource="{Binding Path=Items}">
I have a listbox representing a SQL WHERE statement and the items inside can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.