Write a program which removes duplicate character from String in java?
package programnew; import java.util.*; public class RemoveDuplicateChar { public static void main(String[] args) { String str = "mynameisrakesh"; Set<Character> setObj = new HashSet<>(); StringBuffer sb = new StringBuffer(); for(int i=0;i<str.length();i++) { Character c = str.charAt(i); if(!setObj.contains(c)) { setObj.add(c); sb.append(c); } } System.out.println(sb); } }
Output is – mynaeisrkh