0
0
CsharpProgramBeginner · 2 min read

C# Program to Find First Non Repeating Character

Use a Dictionary<char,int> to count characters, then loop through the string to find the first character with count 1; for example, foreach (var ch in input) if (counts[ch] == 1) return ch;.
📋

Examples

Inputswiss
Outputw
Inputhello
Outputh
Inputaabbcc
OutputNo non-repeating character found
🧠

How to Think About It

First, count how many times each character appears using a dictionary. Then, check the string from start to end to find the first character that appears only once. Return that character or a message if none found.
📐

Algorithm

1
Get the input string.
2
Create a dictionary to store character counts.
3
Loop through each character in the string and update counts.
4
Loop through the string again and find the first character with count 1.
5
Return that character or a message if none found.
💻

Code

csharp
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        string input = "swiss";
        char result = FirstNonRepeatingChar(input);
        Console.WriteLine(result == '\0' ? "No non-repeating character found" : result.ToString());
    }

    static char FirstNonRepeatingChar(string s) {
        var counts = new Dictionary<char, int>();
        foreach (var ch in s) {
            counts[ch] = counts.GetValueOrDefault(ch, 0) + 1;
        }
        foreach (var ch in s) {
            if (counts[ch] == 1) return ch;
        }
        return '\0';
    }
}
🔍

Dry Run

Let's trace the input "swiss" through the code to find the first non repeating character.

1

Count characters

s:3, w:1, i:1

2

Find first with count 1

Check 's' (3), 'w' (1) → found 'w'

CharacterCount
s3
w1
i1
💡

Why This Works

Step 1: Counting characters

We use a dictionary to count how many times each character appears in the string with counts[ch] = counts.GetValueOrDefault(ch, 0) + 1.

Step 2: Finding the first unique character

We loop through the string again and check the count for each character. The first character with count 1 is returned.

Step 3: Return result or fallback

If no character has count 1, we return '\0' and print a message saying no non-repeating character was found.

🔄

Alternative Approaches

Using LINQ
csharp
using System;
using System.Linq;

class Program {
    static void Main() {
        string input = "swiss";
        var result = input.GroupBy(c => c)
                          .Where(g => g.Count() == 1)
                          .Select(g => g.Key)
                          .FirstOrDefault();
        Console.WriteLine(result == '\0' ? "No non-repeating character found" : result.ToString());
    }
}
This approach is concise and uses LINQ but may be less clear for beginners and slightly less efficient due to multiple enumerations.
Using array for ASCII characters
csharp
using System;

class Program {
    static void Main() {
        string input = "swiss";
        char result = FirstNonRepeatingChar(input);
        Console.WriteLine(result == '\0' ? "No non-repeating character found" : result.ToString());
    }

    static char FirstNonRepeatingChar(string s) {
        int[] counts = new int[256];
        foreach (var ch in s) counts[ch]++;
        foreach (var ch in s) if (counts[ch] == 1) return ch;
        return '\0';
    }
}
This method is faster for ASCII strings but uses fixed size memory and is not suitable for Unicode characters.

Complexity: O(n) time, O(n) space

Time Complexity

The program loops through the string twice: once to count characters and once to find the first unique character, so it runs in linear time O(n).

Space Complexity

It uses extra space for the dictionary to store counts, which can be up to O(n) in the worst case if all characters are unique.

Which Approach is Fastest?

Using an array for ASCII characters is fastest due to direct indexing, but dictionary-based or LINQ approaches are more flexible for Unicode.

ApproachTimeSpaceBest For
Dictionary CountingO(n)O(n)Unicode strings, general use
LINQ GroupingO(n)O(n)Concise code, small strings
Array Counting (ASCII)O(n)O(1)ASCII strings, performance critical
💡
Use a dictionary to count characters first, then find the first with count one to solve this efficiently.
⚠️
Beginners often try to find the unique character in one pass without counting, which can miss cases or be inefficient.