- These are some of the common interview question asked.
1)Remove Duplicates from a list
using System.Linq;
using System;
using System.Collections.Generic;
namespace GroupJoin
{
class Program
{
public static void Main()
{
List<string> list = new List<string>() { "anurag","anurag","abc","abc","xyz"};
Console.WriteLine("::Duplicate Strings in a List::");
var duplicateString = list.GroupBy(x => x)
.Where(group => group.Count() > 1)
.Select(group => group.Key);
foreach (var item in duplicateString)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
2)Remove duplicates in a given string
using System.Linq;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace GroupJoin
{
class Program
{
public static void Main()
{
string input = "anurag;anurag;anurag2;anurag3;anurag4";
string output = string.Join(";", input.Split(';').Distinct().ToArray());
Console.WriteLine(output);
Console.ReadLine();
}
}
}
3)Finding Smallest String
using System.Linq;
using System;
using System.Collections.Generic;
namespace GroupJoin
{
class Program
{
public static void Main()
{
List<string> list = new List<string>() { "anurag","abhishek","xyz"};
Console.Write("Small String-> ");
Console.WriteLine(list.OrderByDescending(x => x.Length).Last());
Console.ReadLine();
}
}
}
4)Remove duplicate characters of a string
using System.Linq;
using System;
using System.Collections.Generic;
namespace GroupJoin
{
class Program
{
public static void Main()
{
string s = "Dddduppplicaaate";
Console.Write("Removed Duplicate Characters -> ");
Console.WriteLine(new string(s.ToLower().ToCharArray().Distinct().ToArray()));
Console.ReadLine();
}
}
}
5)Occurrences of string
using System.Linq;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace GroupJoin
{
class Program
{
public static void Main()
{
string s = "This is great.But is this really great ??";
Console.Write("String Occurences(this) -> ");
Console.WriteLine(Regex.Matches(s, "this", RegexOptions.IgnoreCase).Count);
Console.ReadLine();
}
}
}
True For All and ALL(Check if all values are same)
using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQ
{
class Program
{
static void Main(string[] args)
{
List<int> num1 = new List<int> { 2, 2, 2, 2 };
bool evenOrOdd = num1.TrueForAll(x => x == 2);
Console.WriteLine(evenOrOdd);
Console.ReadLine();
}
}
}
Output
This gives the Same output:
bool evenOrOdd= num1.All(x=>x==2);
Will update more on this post :)