Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Grouping elements in a list by ignoring the duplicates
QtWS25 Last Chance

Grouping elements in a list by ignoring the duplicates

Scheduled Pinned Locked Moved Unsolved C++ Gurus
4 Posts 3 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Vinod KuntojiV Offline
    Vinod KuntojiV Offline
    Vinod Kuntoji
    wrote on last edited by
    #1

    HI All,
    I need a solution,

    Given list {1,1,2,0,1,1,0,1,2,2,0,1,2};
    expected result should be ={1,2,0},{1,0},{1,2,2,0},{1}
    each group starting element should be 1.
    each group ending element should be 0.

    C++, Qt, Qt Quick Developer,
    PthinkS, Bangalore

    joeQJ 1 Reply Last reply
    0
    • Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by Pablo J. Rogina
      #2

      @Vinod-Kuntoji it would be great if you could show what your attempts were so far regarding how to tackle this problem. The way you stated your problem seems not to be particularly related to Qt, but just be a mere C++ (or any programming language) issue.

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • Vinod KuntojiV Vinod Kuntoji

        HI All,
        I need a solution,

        Given list {1,1,2,0,1,1,0,1,2,2,0,1,2};
        expected result should be ={1,2,0},{1,0},{1,2,2,0},{1}
        each group starting element should be 1.
        each group ending element should be 0.

        joeQJ Offline
        joeQJ Offline
        joeQ
        wrote on last edited by
        #3

        @Vinod-Kuntoji Hi, friend.

        #include <iostream>
        #include <vector>
        
        using namespace std;
        
        int main(int argc, char *argv[])
        {
            /** Notes: first element must 1, to create new group */
            int myints[] = {1,1,2,0,1,1,0,1,2,2,0,1,2};
            vector<int> veInts(myints,myints + sizeof(myints) / sizeof(int));
        
            vector<vector<int>> groups;
        
            if(veInts.front() != 1){
                cout << "Notes: first element must 1\n";
                return 0;
            }
        
            int groupId = 0;
            for(int &val:veInts){
                if(val == 1){
                    /** new group */
                    vector<int> group;
                    group.push_back(val);
                    groups.push_back(group);
                }else if(val == 0){
                    /** end group */
                    if(groupId < groups.size()){
                        groups.at(groupId).push_back(val);
                        groupId++;
                    }
                }else{
                    /** append val to group */
                    if(groupId < groups.size()){
                        groups.at(groupId).push_back(val);
                    }
                }
            }
        
            for(vector<int> &group:groups){
                if(group.back() != 0){ ///<  to check back value, if not need, just delete for...
                    group.push_back(0);
                }
            }
        
            for(vector<int> &group:groups){
                cout << "group :";
                for(int &val:group){
                    cout << val << "\t";
                }
                cout << endl;
            }
        
            cout << endl;
            return 0;
        }
        

        Just do it!

        1 Reply Last reply
        3
        • Vinod KuntojiV Offline
          Vinod KuntojiV Offline
          Vinod Kuntoji
          wrote on last edited by
          #4

          @joeQ ,@Pablo-J-Rogina

          Thank you..

          C++, Qt, Qt Quick Developer,
          PthinkS, Bangalore

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved