Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static String removeDuplicate(String s)
- {
- char [] temp = s.toCharArray();
- int length =temp.length;
- for (int i=0;i<length;i++)
- {
- for (int j = i+1; j<length;j++)
- {
- if(temp[i]==temp[j])
- {
- int test =j;
- for(int k=j+1; k<length ; k++)
- {
- temp[test] = temp[k];
- test++;
- }
- length--;
- j--;
- }
- }
- }
- return String.copyValueOf(temp).substring(0,length);
- }
- public static String removeDuplicates(String str) {
- int charsCount[] = new int[256];
- for (int i = 0; i < str.length(); i++) {
- char ch = str.charAt(i);
- charsCount[ch]++;
- }
- StringBuilder sb = new StringBuilder(charsCount.length);
- for (int i = 0; i < charsCount.length; i++) {
- if (charsCount[i] > 0) {
- sb.append((char)i);
- }
- }
- return sb.toString();
- }
- public static String removeDuplicates(String str) {
- boolean seen[] = new boolean[256];
- StringBuilder sb = new StringBuilder(seen.length);
- for (int i = 0; i < str.length(); i++) {
- char ch = str.charAt(i);
- if (!seen[ch]) {
- seen[ch] = true;
- sb.append(ch);
- }
- }
- return sb.toString();
- }
- import java.util.Set;
- import java.util.HashSet;
- public static String removeDuplicate(String s)
- {
- StringBuilder sb = new StringBuilder();
- Set<Character> seen = new HashSet<Character>();
- for(int i = 0; i < s.length(); ++i) {
- char c = s.charAt(i);
- if(!seen.contains(c)) {
- seen.add(c);
- sb.append(c);
- }
- }
- return sb.toString();
- }
- public static String removeDuplicate(String s) {
- char[] temp = s.toCharArray();
- int length = temp.length;
- for (int i = 0; i < length; i++) {
- for (int j = i + 1; j < length; j++) {
- if (temp[i] == temp[j]) {
- int test = j;
- for (int k = j + 1; k < length; k++) {
- temp[test] = temp[k];
- test++;
- }
- length--;
- j--;
- }
- }
- }
- return String.copyValueOf(temp).substring(0, length);
- }
- private static final boolean foundIn(char[] temp, int size, char c) {
- for (int i = 0; i < size; i++) {
- if (temp[i] == c) {
- return true;
- }
- }
- return false;
- }
- public static String removeDuplicateY(String s) {
- char[] temp = s.toCharArray();
- int size = 0; //how many unique chars found so far
- for (int i = 0; i < temp.length; i++) {
- if (!foundIn(temp, size, temp[i])) {
- // move the first-time char to the end of the return value
- temp[size] = temp[i];
- size++;
- }
- }
- return new String(temp, 0, size);
- }
- public static String removeDuplicates(String str){
- int checker=0;
- int val=0;
- String out="";
- for(int i =0;i<str.length();i++){
- val = str.charAt(i)-97;
- if((checker & 1<<val) ==0) out +=str.charAt(i)+"";
- checker|=(1<<val);
- }
- return out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement