Last active
August 29, 2015 14:02
-
-
Save marinhoarthur/f539d06dad2d29a8309f to your computer and use it in GitHub Desktop.
Duplicate elements remover
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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