0

How to remove the duplicate characters from the following String using Java toIndex()?String str = “gdjennrfvsbsbdbsb”;

Can anyone please help with a sample code?

9th May 2020, 8:40 AM
Ramya T
5 Antworten
0
toIndex() method.. 🤔From Which class this method? You can do it by converting char array, use Array.sort method, then using loop, only copy unique elements... And also string class contains method that help you, to copy to new string if it return false else don't copy... Share your try here.. So that one help you to complete your task quickly..
9th May 2020, 9:56 AM
Jayakrishna 🇮🇳
0
I'm not getting the output printed properly.
9th May 2020, 11:52 AM
Ramya T
0
I tried: import java.util.Arrays; public class Program { public static void main(String[] args) { String str = "gdjennrfvsbsbdbsb"; char s[] =str.toCharArray(); Arrays.sort(s); str=""; System.out.println(String.valueOf(s)); for(int i=0; i<s.length; i++) if(str.contains(""+s[i])) continue; else str=str+s[i]; System.out.println(str); } } But thought this is enough.. In other way.. : public class Program { public static void main(String[] args) { String str = "gdjennrfvsbsbdbsb"; String s=""; System.out.println(str); for(int i=0; i<str.length(); i++) if(s.contains(""+str.charAt(i))) continue; else s=s+str.charAt(i); System.out.println(s); } }
9th May 2020, 12:10 PM
Jayakrishna 🇮🇳
0
Ramya T because you're running java code in c# file.. Why? Won't work.. Are you confused..? Corrected code: public class RemoveRepeated4rmString { public static void main(String[] args) { String str = "abbccddedghg"; System.out.println("Befor removing the duplicate chars from the string: " +str); StringBuilder s = new StringBuilder(str); for(int i=0; i<s.length(); i++) for(int j=i+1; j<s.length(); j++){ if(s.charAt(i) == s.charAt(j)) s.delete(j,j+1); } System.out.println("After removing duplicate chars from the string: " +s); } }
9th May 2020, 12:13 PM
Jayakrishna 🇮🇳