Find First Non-Repeating Character In Given String

Introduction

In this blog, we will study how we can find first non-repeating character in a string.

Given below code snippet will help you to understand how we can find the non-repeated character in a string.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpAdvanncedProgramming
{
    public static class FindFirstNonRepCharInString
    {
        public static void FindNonRepCharacter(string Input)
        {
			try
			{
                for (int i = 0; i < Input.Length; i++)
                {
                    if (Input.IndexOf(Input[i], Input.IndexOf(Input[i]) + 1) == -1)
                    {
                        Console.WriteLine("Non Repeated Character Is" + ":" + Input[i]);
                        break;
                    }
                }
                return;
            }
			catch (Exception ex)
			{
                Console.WriteLine("Error Details:" + ex.Message);
			}
        }
    }
}

Main Class Code

using CSharpAdvanncedProgramming;
string Input = "Mudassar Ali";

FindFirstNonRepCharInString.FindNonRepCharacter(Input);

Program Input