The idea here is to initialize an array with some default value , for example 0. And when you come across a particular character in the string, you just increase the counter using the ASCII value of that character in the array you defined initially.
As you pass through the string, you have to update the respective counters for the character in the array. Now one will require to go through the array one more time, and check if there is any value which is equal to 1, if there is - Just return that character as a first non-repeated character in a given string.
private static char ReturnFirstUniqueChar(string sampleString)
// Initialize a sample char array and convert your string to char array.
char samplechar = sampleString.ToCharArray();
// The default array will have all value initialized as 0 - it's an int array.
int charArray = new int;
// Go through the loop and update the counter in respective value of the array
for (int i = 0; i < samplechar.Length; i++)
charArray[samplechar[i]] = charArray[samplechar[i]] + 1;
// One more pass - which will provide you the first non-repeated char.
for (int i = 0; i < charArray.Length; i++)
if (charArray[samplechar[i]] == 1)
// Code should not reach here. If it returns '\0'
// that means there was no non-repeated char in the given string.
static void Main(string args)
Console.WriteLine("The First Unique char in given String is: " +
I have provided just a sample code. It doesn't include error check and edge cases.
For those who are Interested to know the time complexity of the give algorithm - It is
O(N) + O(N) = O(2N) which is nearly O(N). It does use an extra memory space.
Your feedback is welcome.