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

  • Home
  • SEARCH
  • 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 8105403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:09:16+00:00 2026-06-06T00:09:16+00:00

Following are two pieces of code I used to compute power sets of elements

  • 0

Following are two pieces of code I used to compute power sets of elements in a list

code 1)

 public static List<List<int>> getCpower(List<int> list)
    {
        var result = new List<List<int>>();
        for (int i = 0; i < (1 << list.Count); i++)
        { 
            var sublist = new List<int>();
            for (int j = 0; j < list.Count; j++)
            {   if ((i & (1 << j)) != 0)
                {   sublist.Add(list[j]); 
                }
            }
            result.Add(sublist); 
        }

        return result;
    }

code 2)

public static List<List<int>> getCpower(List<int> list)
    {
        var result = new List<List<int>>();var sublist = new List<int>();
        for (int i = 0; i < (1 << list.Count); i++)
        { 
            sublist.Clear();sublist.TrimExcess();
            for (int j = 0; j < list.Count; j++)
            {   if ((i & (1 << j)) != 0)
                {   sublist.Add(list[j]); 
                }
            }
            result.Add(sublist); 
        }

        return result;
    }

The first code used a new statement and if i try to find out powersets of list with count 30 then OutOfMemoryException arises.So to save memory i used Clear() and TrimExcess() to get the list as if it were initialized using a new statement in code2. But these two codes return different results. I do not get why is this happening. Please help.

Are the two following two pieces not doing the same thing

   for(....)   
      {
       var sublist = new List<int>();
       for(......)
           {
            //some code
           }
      }

and

 var sublist = new List<int>();
 for(.....)
    {
      sublist.Clear();sublist.TrimExcess();
      for(.... )
      {
      //some code 
      }
    }
  • 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-06-06T00:09:17+00:00Added an answer on June 6, 2026 at 12:09 am

    In your second code, you only have a single nested list – you’re adding several references referring to the same sublist, which is pointless.

    Have you considered that maybe the reason you’re running out of space with your first code is because you’re fundamentally trying to hold too much data in memory at a time?

    You could consider returning an IEnumerable<List<int>> like this:

    public static IEnumerable<List<int>> getCpower(List<int> list)
    {
        for (int i = 0; i < (1 << list.Count); i++)
        { 
            var sublist = new List<int>();
            for (int j = 0; j < list.Count; j++)
            {   if ((i & (1 << j)) != 0)
                {   
                    sublist.Add(list[j]); 
                }
            }
            yield return sublist;
        }
    }
    

    This will now be lazily evaluated – so you could iterate over the top-level sequence, but unless the lists are retained by the caller, you’ll only have a single list in memory at a time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

consider the following two pieces of code: public static Time Parse(string value) { string
Consider the following two pieces of code: var adj=0>grip.y?0<grip.x?0:-180:0<grip.x?-360:-180; and var adj; if (grip.y
I have the following two pieces of code: public class C { public void
The following two pieces of code compile, but I get a connect() failed error
I have following two pieces of code First one is not explicitly disposing Image
i have the following two pieces of code which i think should be identical
What's the difference between the following two pieces of code? Version B seems harder
I have the following two pieces of code, please take a look at it,
Is there a performance difference between the following two pieces of code? if (myCondition)
Consider two following pieces of code - the only difference between them is a

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.