My method for removing the duplicate numbers works but not if a number appears more than twice. e.g. a list with numbers 1,2,2,3,4,5,6,7,7,7,8,9 when using the method gives the list 1,2,3,4,5,6,7,7,8,9
import java.util.*;
public class final SortAndRemove{
private SortAndRemove(){
}
public static void selectionSort(List<Integer> a){
if(a == null)
return;
if (a.size() == 0 || a.size() == 1)
return;
int smallest;
int smallestIndex;
for (int curIndex = 0; curIndex < a.size(); curIndex++) {
smallest = a.get(curIndex);
smallestIndex = curIndex;
for (int i = curIndex + 1; i < a.size(); i++) {
if (smallest > a.get(i)) {
smallest = a.get(i);
smallestIndex = i;
}
}
if (smallestIndex == curIndex);
else {
int temp = a.get(curIndex);
a.set(curIndex, a.get(smallestIndex));
a.set(smallestIndex, temp);
}
}
}
public static void removeDuplicates(List<Integer> a){
if(a == null)
return;
if (a.size() == 0 || a.size() == 1)
return;
for(int curIndex = 0; curIndex <a.size(); curIndex++){
int num = a.get(curIndex);
for(int i = curIndex + 1; i < a.size(); i++){
if(num == a.get(i))
a.remove(i);
}
}
}
}
Wikipedia states that a utility class:
It's good to give your utility class a private constructor (so that it can never be initialised) i.e.
(This is discussed in Effective Java by Joshua Bloch, by the way)