How to Delete Duplicate entries from STL containers?

16 04 2009


If you want to remove duplicate items, you can go for stl::set. But what to do if you want to delete duplicate data from other containers?

removeduplicate
Picture Courtesy – Squidoo


You can use std::unique() algorithm to remove adjacent duplicate items. So at first, sort your data, then call std::unique(). Now all the duplicate data will be rearranged to end of container. Now delete the unwanted range of duplicate data. Have a look at code snippet below.

#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main(int argc, char* argv[])
{
    // Election list.
    vector<string> ElectionList;
    ElectionList.push_back( "Sam" );
    ElectionList.push_back( "John" );
    ElectionList.push_back( "Ron" );
    ElectionList.push_back( "Sam" );
    ElectionList.push_back( "John" );

    // Sort the list to make same items be together.
    sort( ElectionList.begin(), ElectionList.end());

    // Rearrange unique items to front.
    vector<string>::iterator Itr = unique(
        ElectionList.begin(),
        ElectionList.end());

    // Delete the duplicate range.
    ElectionList.erase( Itr, ElectionList.end());
}


Take care that std::unique() just removes the adjacent duplicate entries. It wont remove the entire duplicate entries present in the container. That’s why we need to sort the container at first, which will arrange all duplicate entries to adjacent  locations. 😉


Targeted Audience – Beginners.





Seek uniqueness? Use STL set.

8 05 2008


In several conditions we’ve to worry about avoiding duplicate data. For instance, if you have a combo-box which contains browsed file paths as history, it will be nice to avoid multiple occurrence of same file path. In those cases you can make use of STL set to remove duplicate data.


Set is a template class like any other STL container. For adding new data to set, you’ve to call insert() and for iterating you can use iterators. See sample code snippet below.

#include "set"
#include "string"
using namespace std;
...

// Set which is used to hold FilePaths as strings.
set<string> FilePathSet;

// I am inserting the same path a number of times.
// But finally, while iterating you can see only one
// copy of the same data.
FilePathSet.insert("C:\Windows\System32");
FilePathSet.insert("C:\Windows\System32");
FilePathSet.insert("C:\Windows\System32");
FilePathSet.insert("C:\Windows\System32");

// Iterate through the set.
set<string>::iterator FilePathSetItr = FilePathSet.begin();
while( FilePathSetItr != FilePathSet.end())
{
   // The unique data. Use it.
   string UniqueFilePath = *FilePathSetItr;

   // Increment to next item in set.
   ++FilePathSetItr;
}


Targeted Audience – Beginners.