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:
str
is the input string we are checking for duplicates.len
stores the length of the string using thestrlen()
function.duplicateFound
is a flag variable initialized to 0 and set to 1 if a duplicate is found.- The outer loop iterates over each character using index
i
. - The inner loop, starting at
i + 1
, comparesstr[i]
with subsequent charactersstr[j]
. - If a match is detected,
duplicateFound
is set to 1 and the loops break early. - 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:
str
is the input string to analyze.freq
is an integer array of size 256 (to cover all ASCII characters) initialized to 0, which will store the count for each character.len
holds the length of the string, determined bystrlen()
.- The first
for
loop iterates over each character instr
and increments its corresponding count in thefreq
array. - The second for loop checks every element in the
freq
array to see if any count is greater than 1, indicating a duplicate. - If a duplicate is detected,
duplicateFound
is set to 1 and the loop exits. - 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:
- Nested Loops: Directly compares each character with every other character in the string.
- Frequency Array: Uses an auxiliary array to count character occurrences and checks for duplicates.