Labels

algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

Remove duplicate Characters without extra memory in java

/**
* Without using extra(large) memory Complexity O(n^2)
*
@param word
*/
private static void removeDuplicates(char[] word) {
int currentIndex = 1;
for (int i = 0; i < word.length; i++) {
int j;
for (j = 0; j < currentIndex; j++) {
if (word[i] == word[j]) {
break;
}
}
if (currentIndex == j) {
word[currentIndex
++] = word[i];
}

}
while (currentIndex < word.length) {
word[currentIndex
++] = ' ';
}
}

No comments:

Post a Comment

Search 24 Bytes