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.

  1. First, count the frequency of each character using an array of size 26 (since the input contains only lowercase letters).

  2. 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

  1. Empty String: "" โ†’ Returns -1

  2. All Characters Same: "aaaa" โ†’ Returns -1

  3. Single Character: "a" โ†’ Returns 0

  4. All Unique Characters: "abcd" โ†’ Returns 0

  5. Non-Repeating at End: "aabbcd" โ†’ Returns 4

๐Ÿท๏ธ Asked In

  • Amazon

  • Google

  • Adobe

  • Walmart Labs

  • TCS Digital

  • Samsung R&D

๐Ÿ“š References

  1. Problem Origin & Variants

    • LeetCode โ€“ First Unique Character in a String

    • GeeksforGeeks โ€“ Find the first non-repeating character

    • InterviewBit โ€“ First Non-Repeating Character

0
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.