0

Here's a while-loop written in Java. The purpose is to remove all the duplicates from the list. But the loop is not breaking. There might be other mistakes too.

public static <E extends Comparable<? super E>> void removeDuplicates(ArrayList<E> L) {
    ArrayList<E> temp = new ArrayList<E>(L.size());
    ArrayList<Integer> index = new ArrayList<>(L.size());
    int stop = 0;

    while (true) {
        //test for duplicates and save their indexes
        for (int i = 0; i < L.size(); i++) {
            if (!temp.contains(L.get(i))) {
                temp.add(L.get(i));
                index.add(i);
            }
        }
        // if there were duplicates they will be removed
        if (!index.isEmpty()) {
            stop = 1;
            for (int j = 0; j < index.size(); j++) {
                L.remove(index.get(j));
            }
        }
        //if nothing is removed there should be no duplicates and the loop should break
        if (stop == 0) {
            break;
        }
        index.clear();
        temp.clear();
        stop = 0;
    }

} 
denvercoder9
  • 2,979
  • 3
  • 28
  • 41
jvkloc
  • 623
  • 1
  • 5
  • 13
  • 1
    Why don't you use [Easiest way to convert a List to a Set in Java](https://stackoverflow.com/q/1429860/1744774) when a [`Set` "_contains no duplicate elements_"](https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/util/Set.html) by design? – Gerold Broser Sep 14 '19 at 18:59

1 Answers1

2

You need to update the list of items to be removed when they are already present in the temp list. So the index list will contains index of all duplicate elements:

public static <E extends Comparable<? super E>> void removeDuplicates(final ArrayList<E> L) {
final ArrayList<E> temp = new ArrayList<E>(L.size());
final ArrayList<Integer> index = new ArrayList<>(L.size());
int stop = 0;

while (true) {
  //test for duplicates and save their indexes
  for (int i = 0; i < L.size(); i++) {
    if (!temp.contains(L.get(i))) {
      temp.add(L.get(i));
    } else {

      index.add(i);
    }
  }
  // if there were duplicates they will be removed
  if (!index.isEmpty()) {
    stop = 1;
    for (int j = index.size() - 1; j >= 0; j--) {
      L.remove((int) index.get(j));
    }
  }
  //if nothing is removed there should be no duplicates and the loop should break
  if (stop == 0) {
    break;
  }
  index.clear();
  temp.clear();
  stop = 0;
}
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
  • Thanks. I thought that the stop = 0 in the last line of while would do that. Now I tested without it and it doesn't actually change anything. There's still mistakes; the code doesn't remove all the duplicates. It removes only one and leaves the rest of the duplicates to the list. – jvkloc Sep 14 '19 at 18:26
  • Please refer this updated code, you need to update index list in else condition. As well as when you remove element using index, cast it to int, other the overloaded method with Object input parameter will be called. – Gaurav Jeswani Sep 14 '19 at 18:41
  • Now it's working! Thank You very much! ... how do I use the @ -symbol here in the comments? I put it and 'Gaurav Jeswani' in the beginning of my comment but I don't see your username there. – jvkloc Sep 14 '19 at 18:51
  • :D Even I don't know that. I think you can accept the answer than. ;) – Gaurav Jeswani Sep 14 '19 at 18:53
  • Aha, there's also the sign for accepting the answer. Thank You again, very kind of you to help such a beginner like me :) – jvkloc Sep 14 '19 at 18:57