Introduction
The string is one of the most popular topics in programming job interviews. You will barely face a coding interview where the interviewer asks no string-based questions.
A string is a sequence of characters in computer programming, either as a literal constant or variable.

Today, we’ll go over several approaches, ranging from brute force to optimal, for one of Amazon’s frequently asked questions.- “Find the first non-repeating character in a string.”
Problem Statement: Given a string or stream of characters, find the first non-repeating character, which implies getting the first character that is not occurring more than once in the string.
The string will contain characters only from the English alphabet set, i.e., (‘A’ – ‘Z’) and (‘a’ – ‘z’). If there is no non-repeating character, print -1.
Example: ‘cricketscenario’
Here the first non-repeating character is “k.”
Why is “k” the first non-repeating character?
-
The character ‘c’ appears 3 times at the indices 0,3,8, and hence, “c” is repeating.
-
The character ‘r’ is also appearing multiple times at indices 1,12 and, thus, repeating.
-
The character ‘i’ is again appearing at the indices 2,13; thus, “i” is repeating.
-
Now, the character ‘k’ appears once at index 4, and therefore, it is the first character of the string that is not repeating.
- So, we will simply print/return ‘k.’
Without further ado, let’s get started with the solution. Before that, we recommend you should try it on your own here, champ.
There are several methods to get the non-repeating character in a string, and we will see each one by one thoroughly.
Also See, Sum of Digits in C and C Static Function.
Method 1: (Brute force approach)
Use two for loops for traversing and finding the first character that is not repeating.
Algorithm:
-
Take a for loop from zero to the last character of the string.
-
Take another for loop inside this, and check if the current character occurs just once in the string.
-
If the character you are checking occurs only once, return the character and break from the loops.
-
If there is no such character, return -1.
Below, C++ code is given for your better understanding.
Code
#include <bits/stdc++.h>
using namespace std;
void firstnonRepeating(string str)
{
int i,j,c=0;
for(i=0;str[i]!='\0';i++)
{
//initialise count here so that complete string can be checked
//for each character
int count=0;
for(j=0;str[j]!='\0';j++)
{
if(str[i]==str[j])
{
count+=1;
}
}
if(count==1)
{
cout<<"first non-repeating character in the string="<<str[i];
c+=1;
break;
}
}
// If there is no repeating character return -1.
if(c!=1)cout<<-1<<endl;
}
int main()
{
string str="cricketscenario";
firstnonRepeating(str);
return 0;
}
-
Time Complexity: O(n*n): This approach uses nested for loop. Therefore the complexity will be O(n*n).
- Space Complexity: O(1): No extra space is required. Therefore the space complexity will be constant.
If you’re having trouble calculating time and space complexity, check out this blog to learn everything you need to know about time and space complexity.