Given a string as input, remove all chars from the string that appear again. That is, while reading a string if a char has appeared previously it will be removed.
for example:
remove("Hello")="Helo"
remove("World")="World"
remove("Sachin Tendulkar") = "Sachin Tendulkr"
Please comment if you find any mistake.
for example:
remove("Hello")="Helo"
remove("World")="World"
remove("Sachin Tendulkar") = "Sachin Tendulkr"
public class RemoveDuplicateChars { static String testcase1 = "aaaabbbbbbb"; public static void main(String args[]){ RemoveDuplicateChars testInstance= new RemoveDuplicateChars(); String result = testInstance.remove(testcase1); System.out.println(result); } // function to remove duplicates character public String remove(String str){ String strNew=""; int len=str.length(); // if string is null then return null string if(len==0) { return strNew; } strNew=strNew+str.charAt(0); int i=1; //removing the duplicate character while(i<len) { int flag=1; for(int j=0;j<i;j++) { if(str.charAt(i)==str.charAt(j) ) { flag=0; } } if(flag==0) { i++; } else { strNew=strNew+str.charAt(i); i++; } } return strNew; } }
No comments:
Post a Comment