Remove Duplicates

Remove Duplicates

Hard C Strings 32 views
Explanation Complexity

Problem Statement

Remove duplicate characters from a string. Each character must appear only once in the resulting string. Maintain order.

Input Format

A string entered by the user.

Output Format

A string with duplicate characters removed, order maintained.

Example

"programming"
"progamin"

Constraints

• String contains characters

• Order must be preserved

• No extra duplicate characters

Concept Explanation

Each character should appear only once in the result.
Characters are checked one by one and added only if they have not appeared before.

Step-by-Step Explanation

1.Take the input string.

2.Create an empty result string.

3.Traverse the input string from left to right.

4.For each character:

• Check if it already exists in the result string.

5.If the character is not present:

• Add it to the result string.

6.If the character is already present:

• Skip it.

7.Continue until the end of the string.

8.Print the result string with duplicates removed.

Concept Explanation

Each character should appear only once in the result.
Characters are checked one by one and added only if they have not appeared before.

Step-by-Step Explanation

1.Take the input string.

2.Create an empty result string.

3.Traverse the input string from left to right.

4.For each character:

• Check if it already exists in the result string.

5.If the character is not present:

• Add it to the result string.

6.If the character is already present:

• Skip it.

7.Continue until the end of the string.

8.Print the result string with duplicates removed.

Input / Output Format

Input Format
A string entered by the user.
Output Format
A string with duplicate characters removed, order maintained.
Constraints
• String contains characters

• Order must be preserved

• No extra duplicate characters

Examples

Input:
"programming"
Output:
"progamin"

Example Solution (Public)

C
#include <stdio.h>

int main() {
    char str[200];
    int seen[256] = {0};
    int i, index = 0;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    for (i = 0; str[i] != '�'; i++) {
        unsigned char ch = str[i];

        // Skip newline
        if (ch == 'n')
            continue;

        if (seen[ch] == 0) {
            seen[ch] = 1;
            str[index] = ch;
            index++;
        }
    }

    str[index] = '�';   // terminate modified string

    printf("String after removing duplicates: %sn", str);

    return 0;
}

Official Solution Code

#include <stdio.h>

int main() {
    char str[200];
    int seen[256] = {0};
    int i, index = 0;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    for (i = 0; str[i] != '�'; i++) {
        unsigned char ch = str[i];

        // Skip newline
        if (ch == 'n')
            continue;

        if (seen[ch] == 0) {
            seen[ch] = 1;
            str[index] = ch;
            index++;
        }
    }

    str[index] = '�';   // terminate modified string

    printf("String after removing duplicates: %sn", str);

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.