Check if a String Has Duplicate Characters in C

To check if a string has duplicate characters in C, you can use various methods such as nested loops or frequency counting. In this tutorial, we will explore these approaches with clear examples suitable for beginners.


Example 1: Using Nested Loops

This example demonstrates how to use nested loops to compare each character of the string with every other character. We will check if there are any duplicate characters in the string.

main.c

</>
Copy
#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "hello";
    int len = strlen(str);
    int duplicateFound = 0;

    // Compare each character with every other character
    for (int i = 0; i < len; i++) {
        for (int j = i + 1; j < len; j++) {
            if (str[i] == str[j]) {
                duplicateFound = 1;
                break;
            }
        }
        if (duplicateFound)
            break;
    }

    if (duplicateFound)
        printf("Duplicates found\n");
    else
        printf("No duplicates found\n");

    return 0;
}

Explanation:

  1. str is the input string we are checking for duplicates.
  2. len stores the length of the string using the strlen() function.
  3. duplicateFound is a flag variable initialized to 0 and set to 1 if a duplicate is found.
  4. The outer loop iterates over each character using index i.
  5. The inner loop, starting at i + 1, compares str[i] with subsequent characters str[j].
  6. If a match is detected, duplicateFound is set to 1 and the loops break early.
  7. An if statement then prints “Duplicates found” if any duplicate exists, otherwise it prints “No duplicates found”.

Output:

Duplicates found

Example 2: Using a Frequency Array

This example demonstrates how to use a frequency array to count the occurrences of each character in the string. We then check if any character appears more than once.

main.c

</>
Copy
#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "programming";
    int freq[256] = {0};
    int duplicateFound = 0;
    int len = strlen(str);

    // Count frequency of each character
    for (int i = 0; i < len; i++) {
        freq[(int)str[i]]++;
    }

    // Check if any character's frequency is greater than 1
    for (int i = 0; i < 256; i++) {
        if (freq[i] > 1) {
            duplicateFound = 1;
            break;
        }
    }

    if (duplicateFound)
        printf("Duplicates found\n");
    else
        printf("No duplicates found\n");

    return 0;
}

Explanation:

  1. str is the input string to analyze.
  2. freq is an integer array of size 256 (to cover all ASCII characters) initialized to 0, which will store the count for each character.
  3. len holds the length of the string, determined by strlen().
  4. The first for loop iterates over each character in str and increments its corresponding count in the freq array.
  5. The second for loop checks every element in the freq array to see if any count is greater than 1, indicating a duplicate.
  6. If a duplicate is detected, duplicateFound is set to 1 and the loop exits.
  7. An if statement then prints “Duplicates found” if duplicates exist, or “No duplicates found” if all characters are unique.

Output:

Duplicates found

Conclusion

In this tutorial, we explored two methods to check if a string contains duplicate characters in C:

  1. Nested Loops: Directly compares each character with every other character in the string.
  2. Frequency Array: Uses an auxiliary array to count character occurrences and checks for duplicates.