Interviews - C# Corner

Jitendra Kumar
I Have a string Like "nitin" Write to method or logic to get output like n=2 i=2 t=1
By Jitendra Kumar in .NET onDec 11 2014
  • Jitendra Kumar
    Dec, 2014 11

    string input = "nitin";var resultList = input.GroupBy(e => e).Select(g => new { g.Key, Count = g.Count() }).ToList();

    • 2
  • Md. Raskinur Rashid
    Jan, 2015 10

    // For basic understanding (code tested)string str = "nitin";int dynLength = str.Length;int i = 0, j = 1, c = 0; ;for (i = 0; i <= dynLength; i++){for (j = 0; j < str.Length; j++){if(str[0] == str[j]) // determining the occurances{c = c + 1;}}Console.WriteLine(str[0] + " : " + c); // print the character with no. of occurancesstr = str.Replace(str[0].ToString(),""); // remove character which is already counted;dynLength = str.Length; // set dynamic length after remove counted characterc = 0;i = 0;}Console.ReadLine();

    • 1
  • Vaibhav Salwe
    May, 2015 30

    using linq.class Program{static void Main(string[] args){//int cnt = 0; int occ = 0;Console.WriteLine("Enter string");string str = Console.ReadLine();char[] newstr = str.Distinct().ToArray();for (int i = 0; i < newstr.Length; i ){ Console.WriteLine(" {0} = {1} ", newstr[i], str.Count(x => x == newstr[i]));}Console.ReadLine();}}

    • 0
  • Pankaj  Kumar Choudhary
    May, 2015 27

    string str = "nitin";var Mylsit = str.GroupBy(e => e).Select(g => new { g.Key, Count = g.Count() }).ToList();Console.WriteLine(Mylsit[0]);Console.WriteLine(Mylsit[1]);Console.WriteLine(Mylsit[2]);

    • 0
  • Sudhanshu Pandey
    Jan, 2015 8

    string str = "nitin";char[] arr = str.ToCharArray();Dictionary d = new Dictionary();for (int i = 0; i < arr.Length; i++){if (d == null){d.Add(arr[i].ToString(), 1);}else{if (d.ContainsKey(arr[i].ToString())){d[arr[i].ToString()] = d[arr[i].ToString()] + 1;}else{d.Add(arr[i].ToString(), 1);}}}foreach (var s in d){Console.WriteLine(s.Key + "=" + s.Value);}

    • 0

Most Popular Companies

Most Popular Job Functions

MOST LIKED QUESTIONS