Find First Non-Repeating Character in a String


๐ Problem Statement
Given a string A
, find and return the index of the first non-repeating character.
If there is no such character, return -1
.
๐ Constraints
1 โค |A| โค 10โต
A contains only lowercase English letters.
๐ฏ Objective
You are given a string like "aabbcdc"
.
You need to return the index of the first character that does not repeat in the string.
In this example, "d"
is the first non-repeating character, at index 4.
๐ก Approach
We can solve this efficiently in two passes using a frequency count.
First, count the frequency of each character using an array of size 26 (since the input contains only lowercase letters).
Then, iterate over the string again and return the index of the first character with frequency 1.
This approach is O(n) time and O(1) space (because the character set is fixed).
โ C Code
#include <stdio.h>
#include <string.h>
int firstNonRepeatingCharIndex(char *str) {
int freq[26] = {0}; // An integer array named freq of size 26, all initialized to 0
int len = strlen(str);
// count frequency
for (int i = 0; i < len; i++) {
freq[str[i] - 'a']++; // Increment index for that character
}
// find first non-repeating character
for (int i = 0; i < len; i++) {
if (freq[str[i] - 'a'] == 1) {
return i;
}
}
return -1;
}
int main() {
char str1[] = "aabbcdc";
printf("Index: %d\n", firstNonRepeatingCharIndex(str1)); // Output: 5
char str2[] = "aabbcc";
printf("Index: %d\n", firstNonRepeatingCharIndex(str2)); // Output: -1
return 0;
}
๐งช Example
Let's trace through the input "aawdd"
:
Step 1: Frequency Counting
Position 0: 'a' โ freq[0] = 1
Position 1: 'a' โ freq[0] = 2
Position 2: 'w' โ freq[22] = 1
Position 3: 'd' โ freq[3] = 1
Position 4: 'd' โ freq[3] = 2
Frequency Array After First Pass:
freq[0] = 2 (character 'a')
freq[3] = 2 (character 'd')
freq[22] = 1 (character 'w')
All other indices = 0
Step 2: Finding the First Non-Repeating Character
Position 0: 'a' โ freq[0] = 2 (not 1, continue)
Position 1: 'a' โ freq[0] = 2 (not 1, continue)
Position 2: 'w' โ freq[22] = 1 โ (found!)
โ ๏ธ Common Mistakes
Forgetting to check case sensitivity (this solution assumes only lowercase letters).
Returning the character instead of its index.
Using hash maps unnecessarily for small fixed alphabets.
โ ๏ธ Edge Cases
Empty String:
""
โ Returns -1All Characters Same:
"aaaa"
โ Returns -1Single Character:
"a"
โ Returns 0All Unique Characters:
"abcd"
โ Returns 0Non-Repeating at End:
"aabbcd"
โ Returns 4
๐ท๏ธ Asked In
Amazon
Google
Adobe
Walmart Labs
TCS Digital
Samsung R&D
๐ References
Problem Origin & Variants
LeetCode โ First Unique Character in a String
GeeksforGeeks โ Find the first non-repeating character
InterviewBit โ First Non-Repeating Character
Subscribe to my newsletter
Read articles from Divya Vetriveeran directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Divya Vetriveeran
Divya Vetriveeran
I am currently serving as an Assistant Professor at CHRIST (Deemed to be University), Bangalore. With a Ph.D. in Information and Communication Engineering from Anna University and ongoing post-doctoral research at the Singapore Institute of Technology, her expertise lies in Ethical AI, Edge Computing, and innovative teaching methodologies. I have published extensively in reputed international journals and conferences, hold multiple patents, and actively contribute as a reviewer for leading journals, including IEEE and Springer. A UGC-NET qualified educator with a computer science background, I am committed to fostering impactful research and technological innovation for societal good.