Skip to content

Instantly share code, notes, and snippets.

@aviatrix
Created January 16, 2012 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aviatrix/1621632 to your computer and use it in GitHub Desktop.
Save aviatrix/1621632 to your computer and use it in GitHub Desktop.
Calculating density of characters and printing them in hi-to-low manner
/*
sample input : aazzaaffggghhtzzz
sample output : zzzzzaaaagggffhht
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace task1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Въведете низ :");
var text = Console.ReadLine();
var list = new Dictionary<char, int>();
for (int i = 0; i < text.Count(); i++)
{
if (!list.ContainsKey(text[i]))
{
var num = text.Where(x => x.Equals(text[i])).Count();
list.Add(text[i], num);
}
}
foreach (var i in list.OrderByDescending(x => x.Value))
{
for (int j = 0; j < i.Value; j++)
{
Console.Write(i.Key);
}
}
Console.ReadLine();
}
}
}
@h4cky
Copy link

h4cky commented Jan 16, 2012

linq?

@aviatrix
Copy link
Author

what about linq ?

@h4cky
Copy link

h4cky commented Jan 16, 2012

I just answer myself after viewing the imports in begging after writing the comment :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment