hw to find maxm repeated values [Resolved]

Posted by Oswaldlily under C# on 12/1/2011 | Points: 10 | Views : 1228 | Status : [Member] | Replies : 5
1 2 1 3 1 2 1 3 2 3 1 3 which i have stored in list

how to find first maxm repaeated elements here
second maxm repaeated elements
3rd maximum repeated elements




Responses

Posted by: Blessyjees on: 12/1/2011 [Member] Bronze | Points: 50

Up
0
Down

Resolved
Hi,

Try this

var numberGroups = (from n in num

group n by n into g
orderby g.Count() descending
select new { Numbers = g.Distinct() }).Take(1);


Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksingh on: 12/1/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Try below code.
 List<int> num = new List<int>();

// todo: add items to list as mentioned above .....
var numberGroups = from n in num
group n by n into g orderby g.Count() descending
select new {Numbers = g.Distinct() };

foreach (var g in numberGroups)
{

foreach (var n in g.Numbers)
{
Console.WriteLine(n);
}
}



Printed result will be in order to max to min repeated num.

Regards,
Sunil

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 12/1/2011 [Member] Starter | Points: 25

Up
0
Down
@Sksingh

in ur code i m getting all unique values....

I need maximum repeated values
(i.e) first maxm repeated values are 1
second-3
Third-2

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Blessyjees on: 12/1/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,

I have tried sksingh code. I got the correct result

List<int> num = new List<int>();

num.Add(1);
num.Add(2); num.Add(1); num.Add(3); num.Add(1); num.Add(2); num.Add(1);
num.Add(3); num.Add(2); num.Add(3); num.Add(1); num.Add(3);
// todo: add items to list as mentioned above .....
var numberGroups = from n in num
group n by n into g
orderby g.Count() descending
select new { Numbers = g.Distinct() };

foreach (var g in numberGroups)
{

foreach (var n in g.Numbers)
{
Console.WriteLine(n);
}
}

output
1
3
2

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 12/1/2011 [Member] Starter | Points: 25

Up
0
Down
if i need to maximum number alone????...no need to get 2nd and 3rd element??

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response