public static int LinearSearch(int[] A,int val) { for(int i=0;i<A.Length;i++) { if(A[i]==val) return i; } return-1; }
C#
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);
}
}
}
}
Finding substring in a string
Following program finds a substring in a string.
public static int Find(string text, string pattern)
{
// ASHI COMPARE A,S,H,I
// SHI A==S,S==H
for (int i = 0; i <=text.Length – pattern.Length; i++)
{
int temp = i;
for (int j = 0; j < pattern.Length; j++)
{
temp++;
if (text[temp] == pattern[j])
{
j++;
temp++;
if (j == pattern.Length)
return 1;
continue;
}
break;
}
}
return -1;
}
Program to check valid parenthesis
Program to check valid parenthesis
public class Solution { public bool IsValid(string s) { Stack stack=new Stack(); foreach(char ch in s) { if(ch=='(' || ch=='{' || ch=='[') stack.Push(ch); if(ch==')' || ch=='}' || ch==']') stack.Pop(); } if(stack.Count==0) return true; else return false; } }
Delegates in C#
There are different types in C# such as class,interfaces and enums.One of the reference type is delegate.Delegate allows to define objects which can point to a function.This makes it convenient to define decoupled application and pass function as a parameter value to other functions.
For example if you define a delegate called ProcessDelegate then you can create an object of this delegate.This delegate object can point to a method called printMessage which can be passed to a function:
You declare a delegate by using the keyword delegate followed by the signature of the method the delegate will point to:
delegate [return type] Method(parameter list);
by this understanding we can define a delegate called SimpleDelegate:
public delegate int SimpleDelegate(string message);
Now we can declare an object of this delegate as:
SimpleDelegate delObj=simpleMethod;
Now we can call this method as:
delObj("hello world");
If you notice above we are calling the simpleMethod by using the delegate object.This allows to create loosely coupled application and to pass the function reference to other functions as parameters.
If we require we can define the logic to call appropriate function using delegate at runtime as:
if(true)
{
delObj("hello world");
}
else
{
delAnotherObject("Another Message");
}