I am having half an issue with the following code, it is an insertion sort method used with the Comparable interface. In this specific case it is supposed to sort elements in decreasing order, which it does fine. However, I am also trying to remove duplicates within the same method but it is not working. I was wondering whether it is actually possible to do that within the same method? I have looked at the answer at the following question Removing Duplicates Inside Insertion Sort but I am not sure how to apply it in here. Not necessarily looking for the solution but if you can point me into the right direction from where I can take it further. Thanks in advance.
public void InsertionSortDecrease(){
for(int i=1;i<size();i++){
T keyelement = get(i);
int pos=i;
while(pos > 0 &&
(((Comparable)keyelement).compareTo((Comparable)get(pos-1)) > 0)){
set(pos,get(pos-1));
pos--;
}
set(pos,keyelement);
if(((Comparable)get(pos)).compareTo((Comparable)get(pos+1))
== 0){
remove(pos);
}
}
}
equals()for the class of yourkeyObject? Even better, declare the type asT extends Comparable<T>