1
import java.util.*;

public class list<E> extends LinkedList<E> {
  int j=0;
  int l=0;
  LinkedList Search(Object o) {
    int[] array=new int[super.size()];
    for(int i=0; i<super.size(); i++) {
        if(o.equals(super.get(i))) {
            array[j]=i;
            j=j+1;
            continue;
        }
    }
    return Arrayfilter(array);
 }

 private LinkedList Arrayfilter(int[] i) {
    int[] aray=null;
    LinkedList<Integer> ll= new LinkedList<Integer>();
    for(int j:i){
        if((l==0)|(j!=0)){
            ll.add(j);
            l=l+1;
            continue;
        }
    }
    return ll;
 }

 boolean deleteduplicate() {
    for(int l=0; l<super.size(); l++){
      for(int i:Search(super.get(l))) {
        super.remove(i);
      }
    }
    return true;
 }
}

Whenever I tried that method delete duplicate () it gives me index out of bounds error how I can modify the method that it return true

I know that whenever I remove anything in for loop I reducing the size of list that giving me that error, or anything else?

Pramod
  • 1,376
  • 12
  • 21
  • Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – akuzminykh Jun 03 '20 at 11:47
  • 1
    1.) You are using the [*raw type* `LinkedList`](https://stackoverflow.com/q/2770321/2711488) several times. 2) You are notoriously using the opposite of the common coding style, lowerclass class names and upper case method names. This makes the code hard to read for those who want to help you. 3) You may rethink [whether `LinkedList` is the right choice](https://stackoverflow.com/q/322715/2711488) here. 4.) This brings us to one of the reasons you should [*use* collections, not subclass them](https://stackoverflow.com/q/49002/2711488). – Holger Jun 03 '20 at 16:29

2 Answers2

2

If you use LinkedHashSet<E> instead of LinkedList you get that for free.

To keep your code you could use:

void deleteduplicate() {
   Set<E> tmp = new LinkedHashSet<>(this);
   this.clear();
   this.addAll(tmp);
}

Following the example in [this question].(How do I remove repeated elements from ArrayList?)

fjsv
  • 705
  • 10
  • 23
1

You can use a Set to track the duplicates and a listIterator() to traverse the linked list as below

   public LinkedList<E> deleteDuplicate(LinkedList<E>list)
     {
        Iterator<? extends E> it=list.listIterator();
        Set<E> set = new HashSet<>();
        while(it.hasNext())
        {
            E e=it.next();
            if(set.contains(e))
               it.remove();
            else
                set.add(e);
        }
        return list;
     }

EDIT :

public LinkedList<E> deleteDuplicate(LinkedList<E>list)
    {
        Iterator<? extends E> it=list.listIterator();
        Set<E> set = new HashSet<>();
        while(it.hasNext())
        {
            E e=it.next();
            if(!set.add(e))
                it.remove();
        }
        return list;
    }
Debapriya Biswas
  • 1,079
  • 11
  • 23
  • 2
    The contract of `Set.add` is already sufficient to test the presence of an element, so you can simply use `while(it.hasNext()) if(!set.add(it.next())) it.remove();`, without a preceding `contains` test. Or do it even simpler: `Set set = new HashSet<>(); list.removeIf(e -> !set.add(e));`. – Holger Jun 04 '20 at 10:30
  • @Holger added in EDIT : section . Thanks – Debapriya Biswas Jun 05 '20 at 18:53