0
0
CppProgramBeginner · 2 min read

C++ Program to Find First Non Repeating Character

Use a C++ program that counts character frequencies with a std::unordered_map and then finds the first character with count 1 by iterating the string twice.
📋

Examples

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

How to Think About It

First, count how many times each character appears in the string. Then, look at the string from the start and find the first character that appears only once.
📐

Algorithm

1
Get the input string.
2
Create a map to count each character's frequency.
3
Loop through the string and update counts in the map.
4
Loop through the string again and check the count of each character.
5
Return the first character with count 1 or indicate none found.
💻

Code

cpp
#include <iostream>
#include <unordered_map>
#include <string>

int main() {
    std::string s;
    std::getline(std::cin, s);
    std::unordered_map<char, int> freq;

    for (char c : s) {
        freq[c]++;
    }

    for (char c : s) {
        if (freq[c] == 1) {
            std::cout << c << std::endl;
            return 0;
        }
    }

    std::cout << "No non-repeating character found" << std::endl;
    return 0;
}
Output
w
🔍

Dry Run

Let's trace the input "swiss" through the code.

1

Count characters

s:3, w:1, i:1

2

Find first non-repeating

Check 's' count=3 (skip), 'w' count=1 (found)

CharacterFrequency
s3
w1
i1
💡

Why This Works

Step 1: Counting frequencies

We use unordered_map to store how many times each character appears, so we know which are repeated.

Step 2: Finding the first unique

By checking characters in original order, the first with count 1 is the first non-repeating character.

🔄

Alternative Approaches

Using array for frequency (only for ASCII)
cpp
#include <iostream>
#include <string>

int main() {
    std::string s;
    std::getline(std::cin, s);
    int freq[256] = {0};

    for (char c : s) {
        freq[(unsigned char)c]++;
    }

    for (char c : s) {
        if (freq[(unsigned char)c] == 1) {
            std::cout << c << std::endl;
            return 0;
        }
    }

    std::cout << "No non-repeating character found" << std::endl;
    return 0;
}
Faster for ASCII but not suitable for Unicode or large character sets.
Using two loops without extra space
cpp
#include <iostream>
#include <string>

int main() {
    std::string s;
    std::getline(std::cin, s);

    for (size_t i = 0; i < s.size(); ++i) {
        bool unique = true;
        for (size_t j = 0; j < s.size(); ++j) {
            if (i != j && s[i] == s[j]) {
                unique = false;
                break;
            }
        }
        if (unique) {
            std::cout << s[i] << std::endl;
            return 0;
        }
    }

    std::cout << "No non-repeating character found" << std::endl;
    return 0;
}
No extra memory but slower (O(n²)) for large strings.

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

Time Complexity

Counting frequencies and checking characters each take O(n), so total is O(n).

Space Complexity

We use extra space for frequency map proportional to unique characters k.

Which Approach is Fastest?

Using a hash map is fastest for general strings; array method is faster for ASCII but limited; two loops use no extra space but are slower.

ApproachTimeSpaceBest For
Hash Map FrequencyO(n)O(k)General strings with any characters
Array FrequencyO(n)O(1)ASCII strings, faster but limited charset
Two LoopsO(n²)O(1)Small strings or memory-limited environments
💡
Use a hash map to count characters for an efficient solution.
⚠️
Not checking characters in original order can give wrong first unique character.