Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer.

Share This

public static void removeDuplicates(char[] str) { if (str == null) return; int len = str.length; if (len < 2) return; int tail = 1; for (int i = 1; i < len; ++i) { int j; for (j = 0; j < tail; ++j) { if (str[i] == str[j]) break; } if (j == tail) { str[tail] = str[i]; ++tail; } } str[tail] = 0; }

Happy Exploring!

No comments:

Post a Comment