Remove Duplicate Numbers in a java Program

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);

}
}

One thought on “Remove Duplicate Numbers in a java Program

  • July 25, 2016 at 9:33 pm
    Permalink

    Impressive blog. I was glad to come across this since I was also born in New York City. in addition, I appreciate you for traveling to Miami and assisting me to better fully understand roasting food. This made it simpler for me with my admittance to University of Alabama. Getting excited about seeing you in our cottage and catch up with our golf game.

Leave a Reply