I have been trying to solve this task for probably more than an hour already.
I need to remove all the duplicates from a string but the tricky part is that if a letter is duplicated and odd number of times, one copy of that letter should remain in the final string. For example a string of assdafff
should be converted to df
because f is presented odd number of times. I managed to make a program to remove all duplicates but I cant find those that are presented an odd number of times there.
It's important to keep the order of encountered elements in the output string the same like in the input.
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
String x = reader.nextLine();
String ne = "";
StringBuffer buf = new StringBuffer(x.length() -1);
for(int i=0; i<x.length(); i++){
for(int v = 0; v<x.length(); v++){
if((x.charAt(i)==x.charAt(v))&&(i!=v)){
break;
}
if((v==x.length()-1)&&(i!=v)){
ne+=x.charAt(i);
}}
}
if(ne.equals("")){
System.out.println("Empty String");
}else{
System.out.println(ne);
}
}