Commonly we need to find the characters in a string which are not repeated or which occurs only once.We can use the following LINQ example to find such characters.
Though this is a short string but you can see that even in this string all characters are repeated except M,L and O.
We first iterate through the string characters one at a time.Then we compare if the last index of that character is same as the first index.If that is the case then we return that element.
using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQ
{
public class Program
{
public static void Main(string[] args)
{
string examplestring=”THIS IS A SIMPLE STRING TO TEST REPEATING CHARACRTERS”;
var chars=examplestring.Where(ch=>examplestring.IndexOf(ch)==examplestring.LastIndexOf(ch));
foreach(var ch in chars)
{
Console.WriteLine(ch);
}
}
}
}