HashMap<Character,Character> h=new HashMap<>();
for(int i=0; i<str.length(); i++){
h.put(str.charAt(i),str.charAt(i));
}
Set<Character> keys=h.keySet();
Character [] arr=new Character[keys.size()];
keys.toArray(arr);
String ans="";
for(int i=0; i<arr.length; i++) {
ans+=arr[i];
}
In this question I have to remove duplicates from the string entered.It works fine for normal characters but when input is like: o&6nQ0DT$3
i.e. containing special characters then it does not get printed in order.
input: o&6nQ0DT$3
expected output: o&6nQ0DT$3
my output: 0Q3DT$&6no
I am getting the value returned by keyset()
method in a set "keys" (because return type is set internally of keyset()
) after that I created an array of length keys and put the string into it for returning. But its coming in different order.