Remove duplicate characters in a string

Problem Given a string, Write a program to remove duplicate characters from the string. Examples Example 1: Input: s = kodeknight Output: kodenight Example 2: Input: s = k5kc Output: k5c Solution There are multiple approach to solve it. Method 1 : Brute Force Code C void removeDuplicates(char *str) { if (!str) { return; } int len = strlen(str); 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'; } ...

Remove duplicates from an unsorted linked list keeping only one instance

Problem Write code to remove duplicates from an unsorted linked list. FOLLOW UP: How would you solve this problem if a temporary buffer is not allowed? Examples Example 1: 2 -> 3 -> 2 -> 5 -> 2 -> 8 -> 2 -> 3 -> 8 -> null => 2 -> 3 -> 5 -> 8 -> null ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy