Remove Duplicate Numbers in a java Program
public class RemoveDupliates {
public static String removeDuplicate(String str, String ch) {
String localFinalStr = "";
String subStr = "";
String localPreStr = "";
// System.out.println("str.indexOf(ch) = " + str.indexOf(ch));
if (str.indexOf(ch) > -1) {
// Get Sub String from starting to char.
localPreStr = str.substring(0, str.indexOf(ch) + 1);
// System.out.println("localPreStr = " + localPreStr);
// Get Sub String from char to the end of the String.
subStr = str.substring(str.indexOf(ch) + 1);
// System.out.println("subStr = " + subStr);
// replace duplicate chars with empty.
subStr = subStr.replace(ch, "");
localFinalStr = localPreStr + subStr;
} else {
localFinalStr = str;
}
// System.out.println("localFinalStr = " + localFinalStr);
return localFinalStr;
}
public static void main(String[] args) {
String str = "";
String ch = "";
int j = 0;
str = "15071991";
str = "15075077991991";
str = "";
str = "1111";
str = "1";
j = str.length();
for (int i = 0; i < j; i++) {
ch = str.charAt(i) + "";
// System.out.println("Ch = " + ch);
str = removeDuplicate(str, ch);
j = str.length();
}
System.out.println("Final String = " + str);
}
}