Grouping elements in a list by ignoring the duplicates
Unsolved
C++ Gurus
-
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. -
@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.
-
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.@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; }
-
@joeQ ,@Pablo-J-Rogina
Thank you..