Finding the earliest repeating character in a string is a common problem in programming and coding interviews. When we work with strings, sometimes we need to find out which character repeats first.
By “earliest repeating character,” we mean:
The character that comes again in the string, and whose second appearance happens before others.
Problem Statement
You are given a string S of length n. Your task is to find the character that repeats earliest. If no character repeats, return -1.
Examples to Understand the above Problem
Example 1:
Input:
s = "programming"
Output:
r
Why? → Because 'r' appears at index 1 and again at index 4, before , any other character repeats.
Step-by-step explanation:
- Start reading the string from left to right:
p r o g r a m m i n gEach letter has an index (position in the string) starting from 0:0 1 2 3 4 5 6 7 8 9 10 - We want to find the earliest repeating character → the first character that appears again.
- Start checking each character:
'p'→ appears at index 0, no previous occurrence → move on.'r'→ appears at index 1, no previous occurrence → move on.'o'→ index 2, no previous → move on.'g'→ index 3, no previous → move on.'r'→ index 4, we have seen'r'before at index 1 → this is the, first. repeating character.
- Stop here → output is
'r'.
The second occurrence of 'r' comes earlier than the second occurrence of any other character, so it is the answer.
Example 2:
Input:
s = "datascience"
Output:
a
Why? → Because 'a' repeats first (index 1 and 3) before any other character repeats in the string.
Step-by-step explanation:
- Start reading the string from left to right:
d a t a s c i e n c eIndices:0 1 2 3 4 5 6 7 8 9 10 - Check each character one by one:
'd'→ index 0, first time → move on.'a'→ index 1, first time → move on.'t'→ index 2, first time → move on.'a'→ index 3, we have seen'a'before at index 1 → this is the first repeating character.
- Stop here → output is
'a'.
Even though 'c' repeats later at indices 5 and 9, the first character to repeat is 'a' because its second occurrence comes first.
Method 1: Brute force Approach (Using Two Loops)
Brute force Approach is the easiest way to find the earliest repeating character. Here We look at each character one by one, and for each character, we check all the characters that came before it to see if it has appeared already.
Steps By Step Explanation
- Start from the first character of the string.
- For each character, look at all the characters that are before it in the string.
- If you find a character that matches any previous character, return it immediately → this is the first repeating character.
- If you check all characters and find no repetition, return
-1.
Example
String: "programming"
- Check
'p'→ no previous characters → move on. - Check
'r'→ previous characters:'p'→ not repeating → move on. - Check
'o'→ previous characters:'p', 'r'→ not repeating → move on. - Check
'g'→ previous characters:'p', 'r', 'o'→ not repeating → move on. - Check
'r'again → previous characters:'p', 'r', 'o', 'g'→'r'found → stop and return'r'.
Output:
r
First Repeating Character in C – Two Loops Approach
#include <stdio.h>
#include <string.h>
char firstRepChar(char str[]) {
int n = strlen(str);
// Check each character
for (int i = 0; i < n; i++) {
// Compare with all previous characters
for (int j = 0; j < i; j++) {
if (str[i] == str[j]) {
return str[i]; // Found repeating character
}
}
}
return '-'; // No repeating character found
}
int main() {
char str[] = "programming";
char result = firstRepChar(str);
if(result != '-') {
printf("First repeating character: %c\n", result);
} else {
printf("No repeating character found.\n");
}
return 0;
}
Output
First repeating character: r