Archives for May 2020
Find non repeating characters in a string using LINQ
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);
}
}
}
}
Overview of Azure Functions
Azure functions are used to isolated small pieces of logic.These functions are triggered when some triggering even happens.
- Azure fucntions is a Serverless computing technology
- Azure functions can be created using C#,Typescript and many different technologies
- Azure functions are based on triggers,events and isolated code.
- Events cause trigger to fire which causes the function to execute.
Some of the events or triggers which cause Function to execute are:
- HTTP Request
- Schedule
- Blob storage
- Queue storage
- Event Grid
- Event Hub
- Service Bus Queue
- Service Bus Topic
Azure functions is a type of App service.Other examples of App Services are Web App and Logic Apps.The benefit of Azure function is that since it is based on serverless architecture so you don’t need to be concerned with the lower level details of the functions such as mananging the OS and middleware.
This makes it easy not only to develop but also to deploy and scale the application.
Following is a basic function which is executed when http trigger is fired
Check if a string is palindrome in C#
using System;
using System.Collections.Generic;
namespace CSharpAlgos
{
class Program
{
static void Main(string[] args)
{
string name="abczcba";
bool pal=true;
//if the string contains even characters
if(name.Length%2==0)
{
//each character should occur even number of times
Dictionary<char,int> dic=newDictionary<char, int>();
foreach(var ch inname)
{
if(!dic.ContainsKey(ch))
dic.Add(ch,0);
dic[ch]=dic[ch]+1;
}
foreach(KeyValuePair<char,int> kv indic)
{
if(kv.Value%2!=0)
pal=false;
Console.Write(kv.Value);
}
}
else
{
int count=0;
//each character should occur even number of times
Dictionary<char,int> dic=newDictionary<char, int>();
foreach(var ch inname)
{
if(!dic.ContainsKey(ch))
dic.Add(ch,0);
dic[ch]=dic[ch]+1;
}
foreach(KeyValuePair<char,int> kv indic)
{
if(kv.Value%2!=0)
{
count++;
if(count==2)
pal=false;
}
Console.Write(kv.Value);
}
}
Console.WriteLine(pal);
}
}
}