Skip to content

Instantly share code, notes, and snippets.

@marinhoarthur
Last active August 29, 2015 14:02
Show Gist options
  • Save marinhoarthur/f539d06dad2d29a8309f to your computer and use it in GitHub Desktop.
Save marinhoarthur/f539d06dad2d29a8309f to your computer and use it in GitHub Desktop.
Duplicate elements remover
void removeDuplicate(List<?> l)
{
if(l.isEmpty() || l.size() == 1)
{
return;
}
List<Integer> toRemove = new ArrayList<Integer>();
for(int i = 0; i < l.size(); i++)
{
for(int j = i+1; j < l.size(); j++)
{
if(l.get(i).equals(l.get(j)))
{
toRemove.add(j);
}
}
if(!toRemove.isEmpty())
{
int diff = 0;
for (int k = 0; k < toRemove.size(); k++) {
toRemove.set(k, toRemove.get(k)-diff);
diff+= 1;
}
}
for(Integer index : toRemove)
{
l.remove((int)index);
}
toRemove.clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment