Pages

Wednesday, 25 July 2018

How to remove duplicate from a string? Interview qus selenium/java/automation testing experienced

package webTest;

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

class test{
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="test";
System.out.println(RemoveDuplicates(str));
}
public static String RemoveDuplicates(String str) {

Set<Character> sete = new HashSet<>();
StringBuffer sf = new StringBuffer();
for(int i=0;i<str.length();i++) {
Character c=str.charAt(i);
if(!sete.contains(c)) {
sete.add(c);
sf.append(c);
}
}
return sf.toString();

}
}

No comments:

Post a Comment