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.

Advertisements

Actions

Information

4 responses

21 04 2009
Sarath

Good Tip…

23 04 2009
Jijo.Raj

Thanks Sarath 🙂

Regards,
Jijo.

26 06 2009
Matt

This code will not work as intended.

The elements after the iterator “Itr” are undefined; they are not necessarily the “duplicates”.

See: http://www.sgi.com/tech/stl/unique.html It clearly states that: “The iterators in the range [new_last, last) are all still dereferenceable, but the elements that they point to are unspecified.”

12 07 2009
RRaveen

Dear Friend,
I don’t know this is correct place to put this message, anyhow, I’m just writing, if you are think is it’s wrong please ignore.You are articles are nice and excellent concept. I would like to invite to newly launched .NET Programming website the codegain.com on 1st of this June 2009. Currently CodeGain has more than 450 articles within the a month under the followings categories C#, VB.NET,ASP.NET,WPF,WCF,WFF,LINQ,SilverLight, AJAX, JQuery, JavaScript, Sql Servers , Oracle and more. To more list of categories visit the http://www.codegain.com. I have seen you are writing greatest article to web portal, I’m kindly asking you publish your article in codegain.com also and support to grow the CodeGain share this with your friends also. I am expecting good response from you. You can contact me using info@codegain.com.

Thank you
RRaveen
codegain.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s




%d bloggers like this: