I'm trying to figure out on how can i avoid printing duplicate names in my sorting algorithm.
The reason i'm ignoring the vacant string is that the system i'm implementing, is a ticket booking system which displays a list of only names instead of the vacant slots.
Can some help me please ?
String[] name={"fiona","vacant","allen","fiona","david","vacant","vacant"};
for (int i = 0; i <= (name.length-1); i++){
for (int j = i+1; j < name.length; j++ ){
if (name[i].compareTo(name[j])>0) {
String temp = name[i];
name[i] = name[j];
name[j] = temp;
}
}
if (!name[i].equals("vacant")) {
System.out.println(name[i] + " ");
}
}
Just check if the name you are printing is the same as the previous one:
String[] name={"fiona","vacant","allen","fiona","david","vacant","vacant"};
for (int i = 0; i <= (name.length-1); i++){
for (int j = i+1; j < name.length; j++ ){
if (name[i].compareTo(name[j])>0) {
String temp = name[i];
name[i] = name[j];
name[j] = temp;
}
}
if (!name[i].equals("vacant") && (i == 0 || !name[i-1].equals(name[i]))) {
System.out.println(name[i] + " ");
}
}