0
Any one know the remove the duplicate characters from a string by using Java
3 Answers
+ 1
There are multiple methods to do this-
1) using HashMap
Store the character as key and iterate over the string. Use containsKey() to check if a character is already inserted or not.
2) using HashSet
It will store only unique characters.
3) using 2 nested for loops
Store the character in a new array and iterate over the string. If the character is present in the new array then move ahead else insert it.
0
String str="djdjjd";
String new_str;
for (int a=0;a<str.length;a++){
if !(new_str.contains(str.charAt(a))){
new_str+=str.charAt(a);
}
}
- 1
Yup.