0

Any one know the remove the duplicate characters from a string by using Java

21st Jun 2022, 2:04 PM
kottala vikram
3 Respostas
+ 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.
21st Jun 2022, 2:16 PM
Avinesh
Avinesh - avatar
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); } }
21st Jun 2022, 3:06 PM
Satyam Mishra
Satyam Mishra - avatar
- 1
Yup.
21st Jun 2022, 2:16 PM
Justice
Justice - avatar