Saturday, April 26, 2014

Most Occurred Number in Integer Array using Linq using C#

String[] _numbers = { "2", "1", "3", "2", "4", "6", "2", "1", "4", "6", "6", "4", "8", "1", "3", "8" };
var _query2 = from n in _numbers
 group n by n into g
 select new
 {
 number = g.Key,
 count = g.Count()
 };
 var _MaxCount = _query2.Max(a => a.count);
 var mostOccurednumber = _query2
 .Where(g => g.count == _MaxCount)
 .Select(g => g.number);

No comments :

Post a Comment

AJAX, asp, Asp.net, asp.net and sql server security, Asp.net IntemIndex, C#, Css, DataBinder.Eval, DataKeyNames, Datalist, Datapager, DataSet, DataTable, DropDownList, FindControl, gridview, JavaScript, jquery, Listview, Paging, Regex, RegularExpression, Repeater, Server side validation, Sql Server, timer, timercallback, Validation, XML, xmlnode, XPath