Posts

Showing posts from June, 2019

Sort array strings in C#

Array.Sort(v,StringComparer.InvariantCulture); or, v = v.OrderBy(x => x).ToArray();

Remove last character from C# string

ans = ans.Remove(ans.Length - 1);

Using SortedDictionary in C#

In C++ we have map. In C# the equivalent of map is Dictionary. However it's not sorted. So we use SortedDictionary instead. Following is an example of mapping. using System; using System.Collections.Generic; namespace PracticeApp {     class Program     {         static void Main(string[] args)         {             var s = Console.ReadLine()?.Split('+');             var myDictionary = new SortedDictionary<int, int>();             foreach (var item in s)             {                 int a = int.Parse(item);                 myDictionary.TryGetValue(a, out var x);                 myDictionary[a] = x + 1;             }             foreach (var item in myDictionary)             {                 Console.WriteLine(item.Key+" "+item.Value);             }         }     } } Sample input: 3+2+1+1+2+2+3 Sample output: 1 2 2 3 3 2