Friday, 20 April 2018

//wap to remove duplicate character in a string?

package stringdemo;

import java.util.HashSet;
import java.util.Set;

public class RemoveDuplicate {

public static void removeDuplicate(String s1)

   Set<Character> s=new HashSet<Character>();
   StringBuffer sb=new StringBuffer();
char[] ch = s1.toCharArray();
for(char c:ch)
{
if(!s.contains(c))
{
s.add(c);
sb.append(c);
}
}
System.out.println(sb.toString());
}

public static void main(String[] args) {
removeDuplicate("GovindBallabhKhan");
removeDuplicate("mamma iloveu");
}

}

o/p:-
GovindBalbhK
ma iloveu

No comments:

Post a Comment