Search code examples
c++algorithmsortingsplitunique

Split vector to unique and duplicates c++


My goal is to split a vector into two parts: with unique values and with duplicates.

For example I have sorted vector myVec=(1,1,3,4,4,7,7,8,9,9) which should be split into myVecDuplicates=(1,7,4,9) and myVecUnique=(1,4,7,9,3,8). So myVecDuplicates contains all values that have duplicates while myVecUnique contains all values but in a single embodiment.

The order does not matter. My idea was to use unique as it splits a vector into two parts. But I have a problem running my code.

vector<int> myVec(8)={1,1,3,4,4,7,8,9};
vector<int>::iterator firstDuplicate=unique(myVec.begin(),myVec.end());
vector<int> myVecDuplicate=myVec(firstDuplicate,myVec.end());\\here error accures that says ' no match for call to '(std::vector<int>) (std::vector<int>::iterator&, std::vector<int>::iterator)'
vector<int> myVecUnique=myVec(myVec.begin()+firstDuplicate-1,myVec.end());

After running this code I get an error that says (2nd line) 'no match for call to '(std::vector) (std::vector::iterator&, std::vector::iterator)'

Please help me to understand the source of error or maybe suggest some more elegant and fast way to solve my problem (without hash tables)!


Solution

  • Ahh..Too many edits in your question for anyone's liking. Just keep it simple by using map.

    In C++, map comes really handy in storing the unique + sorted + respective_count values.

    map<int, int> m;
    for(auto &t : myVec){
        m[t]++;
    }
    vector<int> myVecDuplicate, myVecUnique;
    for(map<int, int>::iterator it = m.begin(); it != m.end(); it++){
        if(it->second > 1) myVecDuplicate.push_back(it->first);
        myVecUnique.push_back(it->first);
    }
    

    Edit:

    maybe suggest some more elegant and fast way to solve my problem (without hash tables)!

    1. Sort the vector
    2. Traverse through the sorted vector,

    and do

      if (current_value == previous_value){
        if(previous_value != previous_previous_value)
         myVecDuplicate.push_back(current_value);
      }
      else{
          myVecUnique.push_back(current_value);
      }
    

    To start, initialize previous_value = current_value - 1 and previous_previous_value as current_value - 2.