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 9054647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:44:32+00:00 2026-06-16T13:44:32+00:00

I am implementing the famous subsets of a set problem. I think I got

  • 0

I am implementing the famous “subsets of a set” problem. I think I got a good working solution, but it contains duplicates. I was hoping that list.unique() would take of the situation, but since for a set the == operator isn’t defined, it doesn’t work. A set of sets doesn’t fix the situation either (using list of sets now).

Having 80% complete solution, I realize there is a better algorithm out there than one I came with. But I am wondering if there is a clever way to remove the duplicates without completely rewriting the algorithm?

Here’s my code:

MAIN.CPP:

#include "random.hpp"

using namespace std;

int main(void) {

    subsets2();

    getchar();
    return 0;
}

Random.Cpp:

void getSubsets2(set<int> myset, list<set<int> > * ptr, int length) {

    if (length == 1) {
        ptr->push_back(myset);
    }

    else {
        set<int> second(myset);
        set<int>::iterator it;
        ptr->push_back(myset);

        it = myset.begin();
        myset.erase(it);
        it = second.begin();
        ++it;
        second.erase(it);

        getSubsets2(myset, ptr, length - 1);
        getSubsets2(second, ptr, length - 1);
    }
}

void subsets2(void) {
    const int N = 4;
    int myints[N] = {
        88, 33, 23, 22
    };
    set<int> myset(myints, myints + N);
    set<int> set2;

    list<set<int> > mylist;

    list<set<int> > * ptr;
    ptr = & mylist;

    list<set<int> > ::iterator it;
    set<int>::iterator it2;

    getSubsets2(myset, ptr, N);
    mylist.unique();


    for (it = mylist.begin(); it != mylist.end(); ++it) {
        set2 = * it;
        for (it2 = set2.begin(); it2 != set2.end(); ++it2) {
            cout << * it2 << " ";
        }
        cout << "\n";
    }

}

Output:

        22 23 33 88
        23 33 88
        33 88
        88
        33
        23 88
        88
        23
        22 33 88 
        33 88
        88
        33
        22 88
        88
        22
  • 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-16T13:44:33+00:00Added an answer on June 16, 2026 at 1:44 pm

    Unique() removes all consecutive duplicate elements from the container. So need to do sort mylist first before run unique().

       mylist.sort();
       mylist.unique();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Implementing a custom membership provider, there are certain properties such as MinRequiredPasswordLength that only
When implementing Serializable interface, in Java, we need to set it's serialVersionUID long. Is
When implementing a hash table using a good hash function (one where the probability
When implementing the Strategy Pattern, where does one put the code that determines which
Is it a good way to use JQuery UI themes while implementing a high
Implementing Document-View pattern I faced the problem: standard generic collections and dictionaries ( List<T>
While implementing, I came across a problem with Spring Cache Abstraction VS interfaces. Lets
I'm new to the world of IoC and having a problem with implementing it
Im implementing NFS and almoste done but the RFC section 3.3.8 says this in
Im Implementing a video broadcasting website. For that I planned to use silverlight player

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.