You can iterate backwards over the list and then check for each item if there you find a duplicate in the remaining list: if you find a duplicate remove the current item and proceed.
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class ListTest {
@Test
public void listDedup() throws Exception {
List<String> duplicates = new ArrayList<>();
duplicates.add("a");
duplicates.add("a");
duplicates.add("A");
duplicates.add("b");
Assert.assertEquals(4, duplicates.size());
/* iterate backwards, so that we can safely remove items
* from the list that we are iterating over
*/
for (int i = duplicates.size() - 1; i >= 0; i--) {
String item = duplicates.get(i);
// now check the remaining items in the list for duplicates
for (int j = i - 1; j >= 0; j--) {
String possibleDuplicate = duplicates.get(j);
// check for equality: use any comparison operation you like
if (item.equalsIgnoreCase(possibleDuplicate)) {
// this is a duplicate - remove it
duplicates.remove(i);
// break the inner loop because we already have a duplicate
break;
}
}
}
// only "a" and "b" reamin in the list
Assert.assertEquals(2, duplicates.size());
Assert.assertEquals("a", duplicates.get(0));
Assert.assertEquals("b", duplicates.get(1));
}
}