Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. public static String removeDuplicate(String s)
  2. {
  3. char [] temp = s.toCharArray();
  4. int length =temp.length;
  5. for (int i=0;i<length;i++)
  6. {
  7. for (int j = i+1; j<length;j++)
  8. {
  9. if(temp[i]==temp[j])
  10. {
  11. int test =j;
  12. for(int k=j+1; k<length ; k++)
  13. {
  14. temp[test] = temp[k];
  15. test++;
  16. }
  17. length--;
  18. j--;
  19. }
  20. }
  21. }
  22. return String.copyValueOf(temp).substring(0,length);
  23. }
  24.  
  25. public static String removeDuplicates(String str) {
  26. int charsCount[] = new int[256];
  27.  
  28. for (int i = 0; i < str.length(); i++) {
  29. char ch = str.charAt(i);
  30. charsCount[ch]++;
  31. }
  32.  
  33. StringBuilder sb = new StringBuilder(charsCount.length);
  34. for (int i = 0; i < charsCount.length; i++) {
  35. if (charsCount[i] > 0) {
  36. sb.append((char)i);
  37. }
  38. }
  39.  
  40. return sb.toString();
  41. }
  42.  
  43. public static String removeDuplicates(String str) {
  44. boolean seen[] = new boolean[256];
  45. StringBuilder sb = new StringBuilder(seen.length);
  46.  
  47. for (int i = 0; i < str.length(); i++) {
  48. char ch = str.charAt(i);
  49. if (!seen[ch]) {
  50. seen[ch] = true;
  51. sb.append(ch);
  52. }
  53. }
  54.  
  55. return sb.toString();
  56. }
  57.  
  58. import java.util.Set;
  59. import java.util.HashSet;
  60.  
  61. public static String removeDuplicate(String s)
  62. {
  63. StringBuilder sb = new StringBuilder();
  64. Set<Character> seen = new HashSet<Character>();
  65.  
  66. for(int i = 0; i < s.length(); ++i) {
  67. char c = s.charAt(i);
  68. if(!seen.contains(c)) {
  69. seen.add(c);
  70. sb.append(c);
  71. }
  72. }
  73. return sb.toString();
  74. }
  75.  
  76. public static String removeDuplicate(String s) {
  77. char[] temp = s.toCharArray();
  78. int length = temp.length;
  79. for (int i = 0; i < length; i++) {
  80. for (int j = i + 1; j < length; j++) {
  81. if (temp[i] == temp[j]) {
  82. int test = j;
  83. for (int k = j + 1; k < length; k++) {
  84. temp[test] = temp[k];
  85. test++;
  86. }
  87. length--;
  88. j--;
  89. }
  90. }
  91. }
  92. return String.copyValueOf(temp).substring(0, length);
  93. }
  94.  
  95. private static final boolean foundIn(char[] temp, int size, char c) {
  96. for (int i = 0; i < size; i++) {
  97. if (temp[i] == c) {
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103.  
  104. public static String removeDuplicateY(String s) {
  105. char[] temp = s.toCharArray();
  106. int size = 0; //how many unique chars found so far
  107. for (int i = 0; i < temp.length; i++) {
  108. if (!foundIn(temp, size, temp[i])) {
  109. // move the first-time char to the end of the return value
  110. temp[size] = temp[i];
  111. size++;
  112. }
  113. }
  114. return new String(temp, 0, size);
  115. }
  116.  
  117. public static String removeDuplicates(String str){
  118. int checker=0;
  119. int val=0;
  120. String out="";
  121. for(int i =0;i<str.length();i++){
  122. val = str.charAt(i)-97;
  123. if((checker & 1<<val) ==0) out +=str.charAt(i)+"";
  124. checker|=(1<<val);
  125. }
  126. return out;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement