Removing duplicates from a String in Java
Hi friends today we will create an application from which we can be able to remove duplicate value from that particular String it is a small but very important application..
example : suppose my name is Dheeraj and I want to remove all repeated values from this string like inside Dheeraj char 'e' occurs two times ...
public class RemoveduplicateString {
public static void main(String a[]) {
String name = "Dheeraj";
System.out.println(name);
StringBuilder sb = new StringBuilder(name);
for (int i = 0; i < name.length(); i++) {
for (int j = i + 1; j < name.length(); j++) {
if (name.charAt(i) == name.charAt(j)) {
sb.deleteCharAt(j);
}
}
}
System.out.println("After deletion :" + sb + "");
}
}
Output: Dheraj
example : suppose my name is Dheeraj and I want to remove all repeated values from this string like inside Dheeraj char 'e' occurs two times ...
public class RemoveduplicateString {
public static void main(String a[]) {
String name = "Dheeraj";
System.out.println(name);
StringBuilder sb = new StringBuilder(name);
for (int i = 0; i < name.length(); i++) {
for (int j = i + 1; j < name.length(); j++) {
if (name.charAt(i) == name.charAt(j)) {
sb.deleteCharAt(j);
}
}
}
System.out.println("After deletion :" + sb + "");
}
}
Output: Dheraj
Comments
Post a Comment