Skip to content

Instantly share code, notes, and snippets.

@yiyizheliu
Created April 12, 2015 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yiyizheliu/32263f3957b43f013fbb to your computer and use it in GitHub Desktop.
Save yiyizheliu/32263f3957b43f013fbb to your computer and use it in GitHub Desktop.
/*
Suppose the list is a known one. This function is to delete the duplicate elements in the list. I use a vector here to deal
with the deleting part.
*/
list deleteDup(list list1)
{
vector<int> ori_list;
int flag=0,i=0;
int inp1;
list<int>::iterator copytovec;
//change the list into a vector to process
ori_list.resize(list1.size());
for(copytovec=list1.begin();copytovec!=list1.end();copytovec++)
{
ori_list[i]=*copytovec;
i++;
}
//from the bottom to the beginning, check if there is the duplicate one
for(int i=ori_list.size()-1;i>=0;i--)
{
for(int j=0;j<i;j++)
{
if(ori_list[i]==ori_list[j])
{
flag=1;
break;
}
}
if(flag==1)
{
ori_list.erase(ori_list.begin()+i);
flag=0;
}
}
list1.clear();
for(int i=0;i<ori_list,size();i++)
{
list1.push_back(ori_list);
}
return list1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment