Skip to main content

Command Palette

Search for a command to run...

Find First Non-Repeating Character in a String

Published
β€’3 min read
Find First Non-Repeating Character in a String
D

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.

πŸ“ 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

Interview Preperation

Part 2 of 4

Cracking coding interviews at top tech companies like Amazon, Google, and Microsoft demands more than just textbook knowledge; it takes pattern recognition, optimal thinking, and hands-on practice with real interview problems.

Up next

Deleting the Nth Node from the End of a List

Problem Statement Given the head of a linked list, remove the nth node from the end of the list and return its head. Examples Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] Explanation: The 2nd node from the end is the node with value...

More from this blog

C

CS Tales

13 posts